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>

}

4

1 回答 1

0

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

创建一个视图模型:

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

就像您所做的那样创建您的视图,但使用您的视图模型而不是您现在使用的模型 (CIM.Models.customer),然后在您的局部视图中,例如在邮政地址局部视图中执行以下操作:

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

使用您的视图模型创建控制器操作:

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

希望这可以帮助

于 2013-05-03T22:39:28.483 回答