0

主要的 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. 我们应该为地址设置一个类,为两个地址设置两个不同的属性吗?像地址主页 {get;set;} 和地址邮件 {get;set;} 之类的东西。如果是,那么如何实现接下来的事情。
  3. 如何为地址创建部分视图?在使用单独的 HomeAddres 类和 MailAddress 类的两种情况下。或使用单个地址类。
  4. 如何使用上述两种方法在主寄存器视图中声明 partialView。
  5. 如何确保在 [HttpPost] Action Method 中我们可以读取所有值,即 RegisterModel 值已绑定,并且各个地址也已绑定。
4

1 回答 1

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 方法中使用这个

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

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

于 2013-05-06T17:04:16.990 回答