1

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の初心者です。

4

2 に答える 2

2

これを行うための一般的な ASP.NET MVC メソッドは、カスタム型に EditorTemplates と DisplayTemplates を使用することです。

~/Views/Shared に、DisplayTemplatesとの 2 つのフォルダーを作成しますEditorTemplates。DisplayTemplates フォルダーで、モデルの名前 (AddressModel) で部分ビューを作成し、DisplayForテンプレートを作成します。

EditorTemplates フォルダーで、という名前の別の部分ビューAddressModel.cshtmlを作成し、EditorForテンプレートを作成します。

MVC は自動的にテンプレートを使用し、要求しているデータを提供します。

于 2013-05-02T18:53:48.187 に答える
0

ビューで @Html.EditorFor (または表示用に @Html.DisplayFor) を使用します。

@model MyNamespace.Models.RegisterModel
@{
     Layout = "~/Views/_Layout.cshtml";
 }
 @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
 {
  <div id="form">
      @Html.EditorFor(m => m.HomeAddress)
      @Html.EditorFor(m => MailAddress)  
  </div>
 }

パーツに対して別のコントローラー アクションを用意する必要はありません。コントローラーの前に RegisterModel にアドレスを入力するだけです。このような:

[HttpGet]
public ActionResult Register() // this will be the page people see first
{
    var model = new RegisterModel();
    return View(model);  // assuming your view is called Register.cshtml
}


[HttpPost]
public ActionResult Register(RegisterModel model){
    DosomethingWithHomeAddress(model.HomeAddress);
    DosomethingWithMailAddress(model.MailAddress);
    model.IsSaved = true; // some way to let the user knwo that save was successful;
                          // if this is true, display a paragraph on the view
    return View(model);
}
于 2013-05-02T21:47:15.170 に答える