【问题标题】:A Strongly Types Partial View which is dynamically model binded at runtime to 2 different model class强类型局部视图,在运行时动态模型绑定到 2 个不同的模型类
【发布时间】:2013-05-06 16:34:36
【问题描述】:

主要的RegisterModel调用嵌套的HomeAddress和MailAddress模型。

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. 我们是否应该有一个地址类和 2 个不同的属性 两个地址?像 Address home {get;set;} 和 地址邮件{get;set;}。如果是,那么如何实现下一步。
  3. 如何为地址创建局部视图?在这两种情况下 使用单独的 HomeAddres 类和 MailAddress 类。或使用单个地址类。
  4. 如何使用两种方法在主寄存器视图中声明 partialView 如上所述。
  5. 如何确保在 [HttpPost] Action Method 中我们可以阅读所有内容 值,即 RegisterModel 值是绑定的,并且单独的地址 也被绑定。

【问题讨论】:

  • 地址是否需要2种不同的模型?
  • @Necros 如果我们使用一个类,那么它不会变得更复杂吗?我尝试使用一个,但它对我来说更糟,尤其是在 [HttpPost] Action 方法中,我将如何获得 2 个不同的地址?可能是我不知道如何使用单个地址类来实现。

标签: asp.net-mvc partial-views model-binding custom-model-binder strongly-typed-view


【解决方案1】:

有一个单一的地址模型

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

在 Views/Shared/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 方法中使用 this

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

并且地址将绑定到 RegisterModel 的属性。

【讨论】:

  • 我之前尝试过上述方法。有几个问题首先是我不想使用 EditorFor,因为我希望通过将地址中的每个字段设置为 PartialView 来自定义布局,第二件事是嵌套类未绑定在 [HttpPost] 操作方法中。我将不得不使用@html.Action("AdddressAction"),然后在该操作方法中实例化 AddressModel,然后在 [HttpPost] 参数中显式传递不同的嵌套模型。检查一下:-stackoverflow.com/questions/16344503/…
  • 1.您定义局部视图的布局。将它放在 Views/Shared/EditorTemplates 文件夹中,并将其命名为与视图模型 (AdressModel.cshtml) 相同的名称 - 用这个来更新我的答案 2. 嵌套类绑定在 http 帖子中,只要它们被正确命名(即.: 名称为 HomeAdress.Street1) 的输入 - 您可以使用 EditorFor 实现此目的。
猜你喜欢
  • 2011-10-11
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多