0

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?

RKM
  • 89
  • 1
  • 3
  • 15
  • And you do not even have a property named `TXTBOX` in your `SubModel` (`for (int i == 0; i < Model.subModelView.Count; i++) { @Html.LabelFor(m => m.desc) @Html.TextBoxFor(m => m.desc) }`). And your POST method returning a `JsonResult` makes no sense. –  Oct 06 '17 at 08:58
  • I have used a For Loop instead of a Foreach loop, still having the same issue. The list inside the model is null – RKM Oct 06 '17 at 09:04
  • Then you still have not done it correctly –  Oct 06 '17 at 09:04
  • Got it. I have to Html.TextboxFor, DropDownFor and LabelFor instead of @Html.Label etc. Thank you!! – RKM Oct 06 '17 at 09:16

0 Answers0