【问题标题】:using strongly typed partial views inside another Strongly Typed view在另一个强类型视图中使用强类型部分视图
【发布时间】:2013-04-23 09:17:16
【问题描述】:

我有 NameModel 和 RegisterModel 以及 SuperClass 类如下:-

案例 1:- 使用超类

public class SuperClass
{
   public RegisterModel Register{ get; set; }
   public NameModel NameInfo { get; set; }
}

public class NameModel
{
    [Required]
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    [Required]
    public string LastName { get; set; }
}

  public class RegisterModel
    {       
        public NameModel NameInfo{ get; set; }
        [Required]
        public string UserName { get; set; } 
        [Required]
        public string Password { get; set;}
    }

MyNamePartial 强类型视图如下:-

@model MyNamespace.Models.NameModel
@{
    Layout = null;
}
{
    @Html.TextBoxFor(m=>m.FirstName,new { @id="firstName"} )
    @Html.TextBoxFor(m=>m.MiddleName,new { @id="middleName"} )
    @Html.TextBoxFor(m=>m.LastName,new { @id="lastName"} )
}

我的注册视图是注册模型的强类型如下:-

@model MyNamespace.Models.SuperClass
@{
    Layout = "~/Views/_Layout.cshtml";
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
   <div id="form">
       @Html.Partial("NameModel",Model.NameInfo)       
    @Html.TextBoxFor(m=>m.Register.UserName,new { @id="userName"})
    @Html.TextBoxFor(m=>m.Register.Password,new { @id="password"})
    <input type="submit" value="Register" id="btnRegister" />
  </div>
}

上述方法会导致对象引用错误。

案例 2:- 使用 HTML.Action 而没有 SuperClass 尝试使用@Html.Action("MyNamePartialView") 而不是@Html.Partial("NameModel",Model.NameInfo),然后我使用如下控制器操作方法

我的注册视图是注册模型的强类型如下:-

@model MyNamespace.Models.RegisterModel
@{
    Layout = "~/Views/_Layout.cshtml";
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
   <div id="form">
      @Html.Action("MyNamePartialView")

    @Html.TextBoxFor(m=>m.UserName,new { @id="userName"})
    @Html.TextBoxFor(m=>m.Password,new { @id="password"})
    <input type="submit" value="Register" id="btnRegister" />
   </div>
}

注册控制器:-

  public ActionResult MyNamePartialView()
  {            
      return PartialView("MyNamePartial", new NameModel());
  }      

[HttpPost]
[AllowAnonymous]
public ActionResult Register(RegisterrModel model)
{
    @ViewBag.sideMenuHeader = "Create an Account";

    if (ModelState.IsValid)
    {
          //Perform Something
           return View();
    }
         return View();
} 

上述情况不绑定表单上输入的值。它为 NameModel 设置了 null。

我不想使用 EditorFor,因为我必须向助手提供 html 和自定义属性。部分视图的绑定失败。它在注册视图中给我对象引用错误。如上所述,我如何使用具有这种 Model 类层次结构的强类型 Partial 视图?

【问题讨论】:

  • 2 种方式。 1)创建一个包含两个模型的superclass。 2)使用部分带有子动作的方法。子操作将实例化视图模型并将其传递给您的局部视图。
  • 感谢@DaveA。你能解释一下这些选项吗?我没完全明白。
  • 您可以创建一个包含 NameModel 和 RegisterModel 的类并将其传递给您的视图。然后,您可以在使用 RegisterModel 绑定控件时将 @Model.NameModel 传递给您的部分。
  • 或者,您可以为您的部分创建一个子操作。带有子动作的 Partial 使用语法 Html.Action("Action Name") 调用...子动作可以实例化模型并将其传递给局部...

标签: c# asp.net-mvc razor partial-views strongly-typed-view


【解决方案1】:

最简单的方法是使用子操作

@model MyNamespace.Models.Register.SuperModel
@{
    Layout = "~/Views/_Layout.cshtml";
}



@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
   <div id="form">
       @Html.Action("MyNamePartialView")
   </div>
    @Html.TextBoxFor(m=>m.Register.UserName,new { @id="userName"})
    @Html.TextBoxFor(m=>m.Register.Password,new { @id="password"})
    <input type="submit" value="Register" id="btnRegister" />
}

让您的操作帖子接受 2 个类

public ActionResult Register(RegisterModel RegisterInfo, NameModel NameInfo)

【讨论】:

  • 谢谢@DaveA,在@Html.Partial("NameModel",@Model.Name.NameInfo) "Name" 没有 NameInfo。我把它改成了@Html.Partial("NameModel", @Model.Name) 但它在同一个地方因对象引用错误而中断。
  • @user2232861,请查看我的编辑。我的意思是命名父类 SuperModel。更重要的是,请添加您的操作方法,在其中实例化您的类并将它们传递给您的视图。
  • 我确实尝试过@Html.Action("MyNamePartialView") 然后我的操作方法是 public ActionResult MyNamePartialView() { return PartialView("MyNamePartial", new NameModel());它加载正常,但是当我填写后提交表单时,它只将 Register 类与表单值绑定,但 Name 类为空。
  • @user2232861,我很困惑。我看到现在您正在使用子动作,在这种情况下您不需要超级模型。但我没有看到您实际上实例化您的模型类。请将代码添加到您的问题中,而不是评论中。
  • 太棒了!!!有效。这么简单的事情,看起来要花很长时间才能弄清楚。归功于戴夫!!!
猜你喜欢
  • 2012-09-30
  • 2013-07-28
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多