0

有主調用嵌套的HomeAddress和MailAddress模型的RegisterModel。A強類型局部視圖,它在運行時動態模型綁定到2個不同的模型類

public class RegisterModel 
{ 
    Public string FirstName {get; set;} 
    Public string LastName {get; set;} 
    Public HomeAddressModel homeAddress {get; set;} 
    Public MailAddressModel mailAddress {get; set;} 
} 

public class HomeAddressModel 
{ 
    Public string Street1 {get; set;} 
    Public string Street2 {get; set;} 
    Public string State {get; set;} 
    Public string City {get; set;} 
} 

public class MailAddressModel 
{ 
    Public string Street1 {get; set;} 
    Public string Street2 {get; set;} 
    Public string State {get; set;} 
    Public string City {get; set;} 
} 

局部視圖的地址

@model MyNamespace.Models.??? 
@{ 
    Layout = "~/Views/_Layout.cshtml"; 
} 
<div id="Address"> 
    //Street1 
    //Street2 
    //State 
    //City 
</div> 

我將如何定義我Parital觀點,這樣我可以在任何與HomeAddressModel或MailAddressModel運行時綁定。

我的主要註冊查看

@model MyNamespace.Models.RegisterModel 
@{ 
    Layout = "~/Views/_Layout.cshtml"; 
} 
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" })) 
{ 
    <div id="form"> 
    @Html.TextBoxFor("FirstName"); 
    @Html.TextBoxFor("LastName"); 
    //Render Partial View for HomeAddress. 
    //Will provide a checkbox if Mailing Address is different. 
    //Render Partial View for MailAddress. 
    </div> 
} 

public ActionResult Register() 
{ 
    var model = new RegsiterModel(); 
    return View(model); 
} 

[HttpPost] 
public ActionResult Register(RegisterModel model, 
          HomeAddressModel homeAddress, 
          MailAddressModel mailingAddress) 
{ 
     //Do Something with different Addresses 
     return View(); 
} 

有5個部分對這個問題: -

  1. 是在RegisterModel類正確創建?這是他們的方式 我們可以嵌套它們嗎?
  2. 對於兩個地址,我們是否應該爲Address和兩個不同的屬性 單個類? somethign like Address home {get; set;} and Address mail {get; set;}。如果是,那麼如何實現接下來的事情。
  3. 如何爲地址創建部分視圖?在這兩種情況下,使用單獨的HomeAddres類和MailAddress類的 。或使用單個地址類。
  4. 如何使用上述方法 在主註冊視圖中聲明partialView。
  5. 如何確保在[HttpPost]操作方法中,我們可以讀取所有的 值,即RegisterModel值被綁定,並且個別地址 也被綁定。
+0

是否有必要有2個不同的地址模型? – Necros 2013-05-06 16:47:45

+0

@Necros如果我們使用一個類,那麼它會變得更復雜嗎?我嘗試使用一個,但它爲我特別是在[HttpPost] Action方法中弄得更多,我將如何得到2個不同的地址?可能是我不知道如何使用單個Address類實現。 – user2232861 2013-05-06 16:53:42

回答

1

有地址一樣

public class AddressModel 
{ 
    Public string Street1 {get; set;} 
    Public string Street2 {get; set;} 
    Public string State {get; set;} 
    Public string City {get; set;} 
} 

單一的模式做一個局部視圖它在查看/共享/ EditorTemplates/AddressModel.cshtml

@model MyNamespace.Models.AddressModel 
<div id="Address"> 
    Html.TextBoxFor(m => m.Street1) 
    //Street2 
    //State 
    //City 
</div> 

現在,如果你有你的視圖模型

public class RegisterModel 
{ 
    Public string FirstName {get; set;} 
    Public string LastName {get; set;} 
    Public AddressModel HomeAddress {get; set;} 
    Public AddressModel MailAddress {get; set;} 
} 

只需爲每個視圖渲染部分視圖解決這樣的

<div id="form"> 
    @Html.TextBoxFor(m => m.FirstName); 
    @Html.TextBoxFor(m => m.LastName); 
    @Html.EditorFor(m => m.HomeAddress) // !! -> this will render AdressModel.cshtml as a partial view, and will pass HomeAdress to it 
    //Will provide a checkbox if Mailing Address is different. 
    @Html.EditorFor(m => m.MailAddress) 
</div> 

可選你可以用調用EditorFor到自己的輔助方法,如果你需要更多的邏輯(視圖或類似的東西附加參數)

在HttpPost方法使用該

[HttpPost] 
public ActionResult Register(RegisterModel model) 
{ 
} 

並且地址將綁定到RegisterModel的屬性。

+0

我以前試過以上方法。有幾個問題首先是我不想使用EditorFor,因爲我想通過使其成爲PartialView來實現地址中每個字段的自定義佈局,第二件事是嵌套類不會綁定在[HttpPost]操作方法中。我將不得不使用@ html.Action(「AdddressAction」),然後在該動作方法中實例化AddressModel,然後在[HttpPost]參數中顯式傳遞不同的嵌套模型。 CHekc這出: - http://stackoverflow.com/questions/16344503/single-strongly-typed-partial-view-for-two-similar-classes-of-different-types – user2232861 2013-05-06 17:12:01

+1

1.你定義的局部佈局視圖。將它放在Views/Shared/EditorTemplates文件夾中,並將其命名爲視圖模型(AdressModel.cshtml) - 用這個更新我的答案 2.嵌套類綁定在http post中,只要它們的名稱正確(即:名稱爲HomeAdress.Street1的輸入) - 使用EditorFor實現此目的。 – Necros 2013-05-06 17:23:19

+0

它工作...非常感謝!我真的不知道EditorFor的用處是什麼。 – user2232861 2013-05-06 18:04:37