Pass multiple model to controller in ActionResult Create

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Pass multiple model to controller in ActionResult Create



I have create four models, then i have created a viewmodel that contains first four, i have created a view with controller that show this viewmodel;



When i created Actioresult Create (http post), i on't know how pass entire model.



viewModel:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Avvisi.Models

public class PianoCompletoViewModels

public Guid Id get; set;
public DatiPiano DatiPiano get; set;
public DescrizionePiano descrizionePiano get; set;
public ImpresaBeneficiaria impresaBeneficiaria get; set;
public ResponsabilePiano responsabilePiano get; set;
public ProspettoFinanziario prospettoFinanziario get;set;




controller:


public ActionResult Create()

string UserID = Convert.ToString(User.Identity.GetUserId());
int AvvisoID = Convert.ToInt32(Session["AvvisoID"]);
ViewBag.FKCompartoID = new SelectList(db.Comparto, "CompartoID",
"Nome");
ViewBag.PianoID = new SelectList(db.DescrizionePiano,
"DescrizioneID", "PresentImpBenef_RelContAziend");
return View();

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PianoCompletoViewController PianoView)

if (ModelState.IsValid)

return View();

return View();



I expect that when i click on submit there is a model filled wiht property(other mmodels) For Example:


PianoView.model1.xxx
PianoView.model2.xxx
PianoView.model3.xxx
PianoView.model4.xxx



cshtml view:


@model Avvisi.Models.PianoCompletoViewModels

@
ViewBag.Title = "Create";


<h2>Dati Piano</h2>

@using (Html.BeginForm())

@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Dati Piano</h4>

@Html.ValidationSummary(true, "", new @class = "text-danger" )

<div class="row" style="margin-left:0px;margin-right:0px">
<div class="form-group">
@Html.LabelFor(model => model.DatiPiano.NomePiano, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.DatiPiano.NomePiano, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.DatiPiano.NomePiano, "", new @class = "text-danger" )
</div>
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.descrizionePiano.PresentImpBenef_RelContAziend, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.descrizionePiano.PresentImpBenef_RelContAziend, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.descrizionePiano.PresentImpBenef_RelContAziend, "", new @class = "text-danger" )
</div>
</div>

<div class="row">
<div class="form-group">
@Html.LabelFor(model => model.impresaBeneficiaria.CompanyName, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.impresaBeneficiaria.CompanyName, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.impresaBeneficiaria.CompanyName, "", new @class = "text-danger" )
</div>
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.responsabilePiano.Nome, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.responsabilePiano.Nome, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.responsabilePiano.Nome, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" style="width:95%;background: #dedede;" />
</div>
</div>
</div>



I don't understand why it no let me to choose right properties(Models).



Can you help me, please???





how your view looks like?
– Ehsan Sajjad
Aug 10 at 13:41





I have updated my main post with cshml.
– Fabio
Aug 12 at 10:47




1 Answer
1



You have to use html control name like this:


<input name="descrizionePiano.someProperty">



Then surely you have got value for it.





No problem doesn't in html, i have updated my main post; Problem is in submit "event" when httpost is fired for the create view, i'd want pass my view model, but i cannot access its property. if i write public ActionResult Create (PianoCompletoViewController pianoView) i expect that pianoview contains property of model, in this case i'd want pianoview.datipiano, pianoview.descrizionepiano etc... but when i write pianoview after dot notation there isn't properties.
– Fabio
Aug 12 at 10:50







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard