![](/img/trans.png)
[英]Create a Customer from a Guest - using MVC3, C#, Entity Framework, Razor Views, SQL Server
[英]MVC3/C#/Razor/EF handling data from multiple partial 'create views'
為了注冊新客戶,我在視圖上有幾個部分,它們引用數據庫中的相應表。 主鍵是標識字段,這就是為什么視圖上沒有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; }
}
創建視圖就像完成操作一樣,但是使用viewmodel而不是現在使用的模型(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);
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.