简体   繁体   English

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

[英]MVC3/C#/Razor/EF handling data from multiple partial 'create views'

For registering a new customer I have several partials on my view which refer to corresponding tables in my database. 为了注册新客户,我在视图上有几个部分,它们引用数据库中的相应表。 Primary keys are identity fields, that's why no IDs on the view. 主键是标识字段,这就是为什么视图上没有ID的原因。 To insert a new customer I need to insert 3 occurrences of address(visiting, postal and contact person's), then insert a row for contract, contact_person(with using addressId from one of already inserted addresses) and finally proceed with inserting a new customer which will contain foreign keys referencing to just inserted visiting and postal addresses, contract and contact person. 要插入新客户,我需要插入3次出现的地址(访问,邮政和联系人的地址),然后插入合同行contact_person(使用已插入地址之一的addressId),最后继续插入新客户将包含引用刚刚插入的访问和邮政地址,合同和联系人的外键。

Could you recommend the best/easiest way to pass the data from these partial views as objects to CustomerController and handle the objects there please? 您能推荐最好/最简便的方法将数据从这些局部视图作为对象传递给CustomerController并在那儿处理这些对象吗? Links to examples of handling similar situations will be also highly appreciated. 链接到处理类似情况的示例的链接也将受到高度赞赏。

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

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

View code: 查看代码:

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

} }

you can wrap your view data in a viewmodel, then when you submit your data it will be mapped to your viewmodel, something like this: 您可以将视图数据包装在视图模型中,然后在提交数据时将其映射到您的视图模型,如下所示:

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

Create your view just like you have done but by using your viewmodel instead of the model you are using now (CIM.Models.customer), and then in your partials, for example in the postal address partial view do something like this: 创建视图就像完成操作一样,但是使用viewmodel而不是现在使用的模型(CIM.Models.customer),然后在局部视图中(例如在邮政地址局部视图中)执行以下操作:

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

Create your controller actions by using your viewmodel: 通过使用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);
    }

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM