一个强类型分部视图,它在运行时被动态地绑定到2个不同的模型类



有一个主要的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>

我将如何定义我的资本视图,以便我可以绑定它在运行时与HomeAddressModel或MailAddressModel。

My main Register View

@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();
}

这个问题有五个部分:-

  1. RegisterModel类创建正确吗?这就是他们的方式我们可以嵌套它们吗?
  2. 我们应该有一个类地址和2个不同的属性两个地址?比如地址home {get;set;}和地址邮件{获取;设置;}。如果是,那么如何实现下一个目标。
  3. 如何为地址创建部分视图?在这两种情况下都是这样使用单独的homeaddress类和MailAddress类。
  4. 如何用两种方法在主注册视图中声明partialView如上所述。
  5. 如何确保在[HttpPost]的动作方法我们可以读取所有值,即RegisterModel值被绑定,以及单个地址

为地址设置一个单独的模型,例如

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的调用包装到您自己的helper方法中

在HttpPost方法中使用

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

和地址将绑定到RegisterModel的属性。

相关内容

  • 没有找到相关文章

最新更新