mvc / c# /Razor/EF处理来自多个局部'create视图'的数据
本文关键字:create 视图 数据 EF Razor 处理 mvc 个局 | 更新日期: 2023-09-27 18:11:11
为了注册一个新客户,我在我的视图上有几个部分,它们引用数据库中相应的表。主键是标识字段,这就是视图上没有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>
}
你可以把你的视图数据包装在一个视图模型中,然后当你提交你的数据时,它将被映射到你的视图模型,像这样:
创建视图:
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)
......
使用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);
}
希望能有所帮助