I am an experienced .NET C# Software Developer, however only a few months ago i started worked with MVC Razor (MVC 5).
I have a small situation that i couldn't find any answer to on the net(after hours of searching)
I have a Model that has a list of another Model which in turn also has a list of a model as shown below.
public class Parent
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public List<GrandChild> GrandChildren { get; set; }
}
public class GrandChild
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
and my Controller has 2 methods, one is main Get, and the other one is post in order to post new data
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult PostParent(Parent parent)
{
if (ModelState.IsValid)
{
//Do an insert to the DB
return View("~/Views/Home/Index.cshtml");
}
return Json("Error");
}
However in my View in Form, i cannot find a way to create an add button and insert new data to the list of Children and GrandChildren(incase of a Child)
@using (Html.BeginForm("PostParent", "Home", FormMethod.Post, new {@class = "form-horizontal", role = "form"}))
{
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
}
I can only add fields for primitive type properites, but not for Objects.
I would really appreciate any Help!