MVC3 / C#/ Razor / EF处理来自多个部分'创建视图的数据'

时间:2013-04-26 11:26:52

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

为了注册一个新客户,我在我的视图中有几个部分参考我的数据库中的相应表。 主键是标识字段,这就是视图上没有ID的原因。要插入新客户,我需要插入3个地址(访问,邮政和联系人),然后为合同插入一行,contact_person(使用已插入地址之一的addressId),最后继续插入新客户将包含引用刚刚插入的访问和邮政地址,合同和联系人的外键。

您是否可以推荐最佳/最简单的方法将数据从这些部分视图作为对象传递给CustomerController并在那里处理对象? 处理类似情况的例子的链接也将受到高度赞赏。

我的页面图片: http://i1345.photobucket.com/albums/p673/swell_daze/customer_registration_zps563341d0.png

表格图片: http://i1345.photobucket.com/albums/p673/swell_daze/tables_zpsc60b644a.png

查看代码:

@model CIM.Models.customer

<h2>Register new customer</h2>

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>New customer registration</legend>
    <fieldset class="span3">
    <legend>Basic information</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.name, "Name")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.name)
        @Html.ValidationMessageFor(model => model.name)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.groupId, "Customer group")
    </div>
    <div class="editor-field">
        @Html.DropDownList("groupId", String.Empty)
        @Html.ValidationMessageFor(model => model.groupId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.status, "Status")
    </div>
    <div class="editor-field">
    @Html.DropDownList("status")
        @Html.ValidationMessageFor(model => model.status)
    </div>
    </fieldset>

    <fieldset class="span3">
        <legend>Visiting address</legend>
        <div>
            @{
Html.RenderPartial("AddressPartial");
            }
        </div>
    </fieldset>

    <fieldset style="width:270px">
        <legend>Postal address</legend>
        <div>
            @{
Html.RenderPartial("AddressPartial");
            }
        </div>
    </fieldset>

    <fieldset class="span3">
    <legend>Contract</legend>
    <div>
    @{
Html.RenderPartial("ContractPartial");
        }
    </div>
    </fieldset>

    <fieldset class="span3" style="width:540px">
    <legend>Contact person</legend>
    <div>
    @{
Html.RenderPartial("ContactPersonPartial");
        }
    </div>
    <p>
        <input type="submit" class="btn btn-success" value="Submit" style="margin-right:1em"/>
        @Html.ActionLink("Cancel", "Index", "Customer", new {@class="btn btn-danger", @type="button"})
    </p>
    </fieldset>
</fieldset>

}

1 个答案:

答案 0 :(得分:0)

您可以将视图数据包装在viewmodel中,然后当您提交数据时,它将映射到您的viewmodel,如下所示:

创建一个viewmodel:

public class MyViewModel
{
    public string name { get; set; }
    public string someprop1 { get; set; }
    public string someprop2 { get; set; }

    public MyAddress visitingaddress { get; set; }
    public MyAddress postaladdress { get; set; }
    public MyContactAddress contactaddress { get; set; }
}

public class MyAddress
{
    public string town { get; set; }
    public string street { get; set; }
    public string housenumber { get; set; }
    public string postalcode { get; set; }
}

public class MyContactAddress : MyAddress
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string email { get; set; }
    public string phonenumber { get; set; }
}

创建你的视图就像你已经完成但是使用你的viewmodel而不是你现在使用的模型(CIM.Models.customer),然后在你的partials中,例如在邮政地址局部视图中做这样的事情:

.......
@Html.EditorFor(x => x.postaladdress.town)
......

使用viewmodel创建控制器操作:

    public ActionResult Index()
    {
        MyViewModel vm = new MyViewModel();
        //doing something here....

        return View(vm);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel vm)
    {
        //save your data, here your viewmodel will be correctly filled
        return View(vm);
    }

希望这会有所帮助