在asp.net mvc中创建复杂模型

时间:2014-07-31 13:10:45

标签: c# asp.net-mvc entity-framework

如果有人可以提供帮助我会很感激

我有一对多的关系:Customer和他的Addresses。我建立了所有依赖项,我的Customer类有ICollection<Address> Addresses。 问题是当我创建新客户时:

public ActionResult Create()
{
    Customer customer = new Customer();
    Address addr = new Address();
    addr.Customer = customer;
    customer.Addresses.Add(addr);
}

当我从HttpPost方法中的视图中收到模型时:

public ActionResult Create(Customer entity)
{
}

Customer模型是正确的并且填充了值,但其Addresses集合为空。我做错了什么?

我的观点Create.cshtmlmodel.CUSTOMER_NOCustomers表中的PK):

@model DAL.Model.Customer
@using DAL.Model
@using CurrencyControl.Enums
<script src="@Url.Content("~/Scripts/app-ui.js")"></script>
@{
    Layout = null;
    CCEntities context = new CCEntities();
}

@using (Ajax.BeginForm("Create", null, new AjaxOptions() { UpdateTargetId = "mainPane", InsertionMode = InsertionMode.Replace }, new { @class = "viewForm form-horizontal" }))
{
    <button type="submit" class="saveButton">Save</button>
    @Html.TextBoxFor(model => model.CUSTOMER_NO, new { @class = "k-textbox" })
    @Html.EditorFor(model => model.FULL_NAME)
    @Html.Kendo().ComboBox().Name("BRANCH").BindTo((SelectList)(new SelectList(context.Branches, "COMPANY_CODE", "COMPANY_NAME"))).HtmlAttributes(new { style = "width:300px" })
    @Html.Kendo().ComboBox().Name("RESIDENCE").BindTo(Display.EnumToSelectList(typeof(Residence), Model.RESIDENCE)).HtmlAttributes(new { style = "width:300px" })
    @(Html.Kendo().ComboBox().Name("COUNTRY").BindTo((SelectList)(new SelectList(context.Countries, "Alpha2", "NAME"))).HtmlAttributes(new { style = "width:300px" }))
    @Html.EditorFor(model => model.Addresses.FirstOrDefault().CITY)
    @Html.EditorFor(model => model.Addresses.FirstOrDefault().STREET)
}

2 个答案:

答案 0 :(得分:0)

如果您只想输入第一个地址的城市和街道

@model DAL.Model.Customer
@using DAL.Model
@using CurrencyControl.Enums
<script src="@Url.Content("~/Scripts/app-ui.js")"></script>
@{
    Layout = null;
    CCEntities context = new CCEntities();
}

@using (Ajax.BeginForm("Create", null, new AjaxOptions() { UpdateTargetId = "mainPane", InsertionMode = InsertionMode.Replace }, new { @class = "viewForm form-horizontal" }))
{   
    {<button type="submit" class="saveButton">Save</button>}

    {@Html.TextBoxFor(model => model.CUSTOMER_NO, new { @class = "k-textbox" })}

    {@Html.EditorFor(model => model.FULL_NAME)}

    {@Html.Kendo().ComboBox().Name("BRANCH").BindTo((SelectList)(new SelectList(context.Branches, "COMPANY_CODE", "COMPANY_NAME"))).HtmlAttributes(new { style = "width:300px" })}

    {@Html.Kendo().ComboBox().Name("RESIDENCE").BindTo(Display.EnumToSelectList(typeof(Residence), Model.RESIDENCE)).HtmlAttributes(new { style = "width:300px" })}

    {@(Html.Kendo().ComboBox().Name("COUNTRY").BindTo((SelectList)(new SelectList(context.Countries, "Alpha2", "NAME"))).HtmlAttributes(new { style = "width:300px" }))}

    {@Html.Editor("Addresses[0].CITY")}

    {@Html.Editor("Addresses[0].STREET")}
}

否则您需要为Address创建EditorTemplate,并使用

{@Html.EditorFor(model => model.Addresses)}

答案 1 :(得分:0)

问题在于默认MVC模型绑定器的行为。从本质上讲,虽然它可以毫无问题地处理嵌套属性,但它也不会在任何地方处理集合。根据我的经验,最简单的解决方案是在模型中使用数组,并使用标准for循环遍历它。

在你的情况下:

for (int i = 0; i < Model.Addresses.Length; i++) {
    @Html.EditorFor(model => model.Addresses[i].CITY)
    @Html.EditorFor(model => model.Addresses[i].STREET)
}

然后,将在相应的输入中生成正确的名称值,从而允许模型绑定器在服务器上正确地重新生成集合。如果您不希望更改模型以使用数组,请考虑使用专门的视图模型。