I have one scenario in my project like below.
Create Person with two views. One is parent view contains person names and the other is partial view contains address information.
if i click on submit button in parent view, i need to read the data entered in address partial view and pass it to PersonViewModel object for POST action method.
I am unable to AddressViewModel from POST Action method, getting null value. Anyone help me how to resolve this issue.
Thanks in advance.
Please find the sample code below.
ViewModels:
public class PersonViewModel
{
public string FirstName{get;set;}
public string LastName{get;set;}
public AddressViewModel Address{get;set;}
}
public class AddressViewModel
{
public class Address1{get;set;}
public class Address2{get;set;}
}
PersonController
public ActionResult POST(PersonViewModel model)
{
Person person=new Person();
person.FirstName=model.FirstName;
person.LastName=model.LastName;
if(model.Address!=null)
{
person.Address1=model.Address.Address1;
}
return View("Confirmation");
}
AddressController:
public ActionResult Address()
{
return View();
}
Person View:
@model POC.ViewModels.PersonViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div>
@{Html.RenderPartial("~/Views/Shared/_Address.cshtml");}
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Address Partial View:
@model POC.ViewModels.AddressViewModel
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Address1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address2, "", new { @class = "text-danger" })
</div>
</div>
</div>