单个强类型部分视图,用于两个不同类型的类似类

时间:2013-05-02 17:48:20

标签: asp.net-mvc partial-views html-helper strongly-typed-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; }
}

我的主要注册视图强烈输入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回复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的后期操作方法中读取值?还有什么需要修改?我上面添加了几乎所有代码。我是MVC的初学者。

2 个答案:

答案 0 :(得分:2)

执行此操作的典型ASP.NET MVC方法是使用EditorTemplates和DisplayTemplates作为自定义类型。

在〜/ Views / Shared中,创建两个文件夹DisplayTemplatesEditorTemplates。 在DisplayTemplates文件夹中,创建一个包含模型名称的部分视图,即(AddressModel),并创建一个DisplayFor模板。

在EditorTemplates文件夹中,创建另一个名为AddressModel.cshtml的部分视图,并创建一个EditorFor模板。

然后,MVC将自动使用您的模板,并为您提供您要求的数据。

答案 1 :(得分: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);
}