I have a Two Models in my MVC application
Main Models
public class MainModel
{
public string desc{get;set;}
public List<SubModel> subModelView{get;set;}
}
public class SubModel
{
public string Id{get;set;}
public string desc {get;set;}
}
in My View im using the mainmodel
View:
@using ViewModel.MainModel
@using (Html.BeginForm())
{
<table>
<tr>
@Html.EditorFor(model => model.desc)
<tr>
@if(Model.subModelView!= null)
{
@foreach (SubModel subview in Model.subModelView)
{
<tr>
<td>
@Html.Label("lblFieldHeader", @subview.desc)
</td>
<td>
@if (subview.ID == "TXTBOX" || subview.ID== "NUM_TXTBOX")
{
@Html.TextBox("TXTBOX", @subview.desc)
}
</td>
</tr>
}
}
<p>
<input type="submit" value="Create" />
</p>
}
In Controller:
[HttpPost]
public ActionResult Create(MainModel ruleViewModel)
{
//I'm adding it to the database
return Json(result, JsonRequestBehavior.AllowGet);
}
The problem im facing here is When the model is passed to the controller, the values in the MainModel is present but inside it the SubModel list in null. But when I debug before going to the controller the count of the list is 2.
Am i Missing something here?