C# 在另一个强类型视图中使用强类型局部视图

C# 在另一个强类型视图中使用强类型局部视图,c#,asp.net-mvc,razor,partial-views,strongly-typed-view,C#,Asp.net Mvc,Razor,Partial Views,Strongly Typed View,我有NameModel和RegisterModel以及超类,如下所示:- 案例1:-使用超类 public class SuperClass { public RegisterModel Register{ get; set; } public NameModel NameInfo { get; set; } } public class NameModel { [Required] public string FirstName { get; set; }

我有NameModel和RegisterModel以及超类,如下所示:-

案例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>
}
@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>
}
我的注册视图是强类型的注册模型,如下所示:-

@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>
}
@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>
}
上述情况不绑定表单中输入的值。它为NameModel设置null


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

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

@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种方式。1) 创建一个包含两个模型的
超类
。2) 使用带有子操作的分部方法。子操作将实例化视图模型并将其传递给您的分部。谢谢@DaveA。你能解释一下选项吗?我没有完全理解它。您可以创建一个同时包含NameModel和RegisterModel的类,并将其传递给视图。然后,您可以在使用RegisterModel绑定控件时将@Model.NameModel传递给您的partial。或者,您可以为您的partial创建子操作。使用语法Html.action(“action Name”)。。。子操作可以实例化一个模型并将其传递给partial…谢谢@DaveA,在@Html.partial(“NameModel”、@model.Name.NameInfo)“Name”中没有NameInfo。我将其更改为@Html.partial(“NameModel”、@model.Name),但它在同一位置因对象引用错误而中断。@user2232861,请查看我的编辑。我想给家长班的超级名模起名。更重要的是,请添加动作方法,在其中实例化类并将它们传递给视图。我尝试了@Html.action(“MyNamePartialView”),然后我的动作方法是public ActionResult MyNamePartialView(){return PartialView(“MyNamePartial”,new NameModel());}它可以很好地加载,但当我在填写表单后提交表单时,它仅将Register类与表单值绑定,但Name类为null。@user2232861,我很困惑。我知道你现在使用的是儿童动作,在这种情况下,你不需要超级模特。但我看不到您实际实例化了模型类。请将代码添加到您的问题中,而不是添加到评论中。太棒了!!!成功了。这是一件很简单的事情,而且它看起来很难弄清楚。荣誉归于戴夫!!!