I have NameModel and RegisterModel and SuperClass classes as below: -
Case 1: - Using SuperClass
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 strongly typed View is as follows :-
@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"} )
}
My Registration View is strongly typed of Register Model as follows :-
@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>
}
Above approach gives object reference error.
Case 2: - Using HTML.Action and no SuperClass Tried using @Html.Action("MyNamePartialView")instead of @Html.Partial("NameModel",Model.NameInfo),Then I use Controller Action method as below
My Registration View is strongly typed of Register Model as follows :-
@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>
}
Register Controller :-
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();
}
The above case doesnt bind the values entered on form. it sets null for NameModel.
I don't want to use EditorFor as i have to supply html and custom attributes to helpers.The binding for the partial view fails.it gives me object reference error in Registration view. How can i use such strongly typed Partial views with such a such Model class hierarchy as explained above ?
The simplest way to do this is to use a child action
@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" />
}
make your action post accept 2 classes
public ActionResult Register(RegisterModel RegisterInfo, NameModel NameInfo)