我有一個註冊主視圖,顯示兩種不同類型的地址1.家庭地址2.郵寄地址管窺單強類型的不同類型的兩個相似的類
public class RegisterModel
{
public AddressModel HomeAddress { get; set; }
public AddressModel MailAddress { get; set; }
}
public class AddressModel
{
public string Street1 { get; set; }
public string Street2 { get; set; }
public string State { get; set; }
public string City { get; set; }
}
我的主要註冊查看強烈鍵入的RegisterModel如下
@model MyNamespace.Models.RegisterModel
@{
Layout = "~/Views/_Layout.cshtml";
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
<div id="form">
@Html.Action("MyAddressPartial")
@Html.Action("MyAddressPartial")
</div>
}
MyAddressPartialView如下: -
@model MyNamespace.Models.AddressModel
@{
Layout = "~/Views/_Layout.cshtml";
}
<div id="Address">
@Html.TextBoxFor(m=>m.Street1 ,new { @id="Street1 "})
@Html.TextBoxFor(m=>m.Street2,new { @id="Street2"})
@Html.TextBoxFor(m=>m.State ,new { @id="State "})
@Html.TextBoxFor(m=>m.City,new { @id="City"})
</div>
我RegisterController: -
// Have to instantiate the strongly Typed partial view when my form first loads
// and then pass it as parameter to "Register" post action method.
// As you can see the @Html.Action("MyAddressPartial") above in main
// Register View calls this.
public ActionResult MyAddressPartial()
{
return PartialView("MyAddressPartialView", new AddressModel());
}
提交我的主要形式,以低於同一註冊控制器提到的操作方法。
[HttpPost]
public ActionResult Register(RegisterModel model,
AddressModel homeAddress,
AddressModel mailingAddress)
{
//I want to access homeAddress and mailingAddress contents which should
//be different, but as if now it comes same.
}
我不想爲MailingAddress和HomeAddress創建一個單獨的類。如果我這樣做,那麼我將不得不爲每個地址創建兩個單獨的強類型局部視圖。
有關如何重用類和部分視圖並使其動態化並在Action Method Post中讀取其單獨值的任何想法。
編輯1回覆斯科特 - 帕斯科 -
在DisplayTemplates文件夾,我添加以下AddressModel.cshtml
<div>
@Html.DisplayFor(m => m.Street1);
@Html.DisplayFor(m => m.Street2);
@Html.DisplayFor(m => m.State);
@Html.DisplayFor(m => m.City);
</div>
而且在EditorTemplate文件夾,我添加以下AddressModel.cshtml但EditorFor
<div>
@Html.EditorFor(m => m.Street1);
@Html.EditorFor(m => m.Street2);
@Html.EditorFor(m => m.State);
@Html.EditorFor(m => m.City);
</div>
現在我該如何在RegisterView中使用它們,以及如何在控制器的pos中讀取值行動方法?還有什麼需要修改?我已經添加了上面的幾乎全部代碼。我非常喜歡MVC。
請看看我的編輯1回覆scott-pascoe.Please讓我知道還有什麼需要做的。 – user2232861 2013-05-02 20:27:40
@ marcind,請看看這個嗎? – user2232861 2013-05-02 20:33:55
謝謝..我現在意識到,EditorFor幫助。 – user2232861 2013-05-06 18:05:58