註冊一個新客戶我有幾個部分在我的視圖中引用我的數據庫中的相應表。主鍵是標識字段,這就是爲什麼視圖上沒有標識。要插入新客戶,我需要插入3次出現的地址(訪問,郵寄和聯繫人),然後爲合同,contact_person插入一行(使用來自已插入地址之一的addressId),最後繼續插入新客戶將包含引用剛插入的訪問和郵政地址,合同和聯繫人的外鍵。MVC3/C#/ Razor/EF處理來自多個部分'創建視圖'的數據
您能否推薦最好/最簡單的方式將這些部分視圖的數據作爲對象傳遞給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>
}
不知道我完全理解這個問題,但不能用隱藏字段來區分這三個地址嗎? – KingCronus 2013-04-26 11:31:12
我想要的是將包含多個部分視圖的視圖中的數據作爲對象傳遞給控制器,而不是字段,因爲在這種情況下,在我的控制器的Create方法中將會有大約20個參數。情況清楚地顯示在圖片http://i1345.photobucket.com/albums/p673/swell_daze/customer_registration_zps563341d0.png – Bobby 2013-04-26 14:46:57