在ActionResult Create中将多个模型传递给控制器

时间:2018-08-10 13:37:31

标签: c# model-view-controller actionresult multiple-models

我创建了四个模型,然后创建了一个包含前四个模型的视图模型,并创建了一个带有该视图模型的控制器的视图;

当我创建Actioresult Create(http发布)时,我不知道如何传递整个模型。

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; }
}
}

控制器:

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();
}

我希望当我单击“提交”时,有一个带有属性的模型(其他mmodels),例如:

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

cshtml视图:

@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>
}

我不明白为什么不让我选择正确的属性(模型)。

请问您能帮我吗?

1 个答案:

答案 0 :(得分:0)

您必须像这样使用html控件名称:

<input name="descrizionePiano.someProperty">

那么您肯定会为此感到有价值。