2 つの異なるタイプの住所を示す Register Primary View があります。 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; }
}
私のメインの Register View は、次のように 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());
}
メイン フォームを同じ Register Controller の以下のアクション メソッドに送信します。
[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 用に別のクラスを作成したくありません。その場合、アドレスごとに 1 つずつ、厳密に型指定された 2 つの個別の部分ビューを作成する必要があります。
クラスと部分ビューを再利用して動的にし、Action Method Post で個別の値を読み取る方法に関するアイデア。
編集 1 scott-pascoe に返信:-
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 でそれらを使用する方法と、Controller の post Action Method で値を読み取る方法を教えてください。他に何を変更する必要がありますか? 上記のほぼすべてのコードを追加しました。私はMVCの初心者です。