2013-04-26 54 views
1

註冊一個新客戶我有幾個部分在我的視圖中引用我的數據庫中的相應表。主鍵是標識字段,這就是爲什麼視圖上沒有標識。要插入新客戶,我需要插入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> 

}

+0

不知道我完全理解這個問題,但不能用隱藏字段來區分這三個地址嗎? – KingCronus 2013-04-26 11:31:12

+0

我想要的是將包含多個部分視圖的視圖中的數據作爲對象傳遞給控制器​​,而不是字段,因爲在這種情況下,在我的控制器的Create方法中將會有大約20個參數。情況清楚地顯示在圖片http://i1345.photobucket.com/albums/p673/swell_daze/customer_registration_zps563341d0.png – Bobby 2013-04-26 14:46:57

回答

0

你可以在一個視圖模型換你查看數據,那麼當你提交你的數據時,它將被映射到你的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; } 
} 

創建視圖,就像你做了,但通過使用您的視圖模型,而不是你現在(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); 
    } 
例如在郵政地址局部視圖做這樣的事情

希望這有助於

相關問題