0

I have a list object within my Model in which I am trying to bind in my post action. However, the list is always null when I post. Please help on how to correct this. I have the following Model:

My Model vw_FormInfo.cs

public partial class vw_FormInfo
{
    ...
    public List<vw_SectionQuestion> SectionQuestion { get; set; }
}

vw_SectionQuestion.cs

public partial class vw_SectionQuestion
{
    ...
    public int SectionID { get; set; }
    public string SectionName { get; set; }
    public string SectionDesc { get; set; }
    public string Description { get; set; }
    ...
}

My View Index.cshtml

@model EvalTool.Models.vw_FormInfo
    @{
    var formSections = Model.SectionQuestion.GroupBy(item => new { item.SectionID, item.SectionName, item.SectionDesc });
    };
    ...
    @using (Html.BeginForm("Edit", "FormInfo", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        ...
        @{int i = 0,j = 0; }
        <table id="dataTables-survey" class="compact table table-striped table-bordered table-hover dataTables">
        @foreach (var formSection in formSections)
        {
            <thead>
               <tr>
                 <th></th>
                 <th>@formSection.Key.SectionID @formSection.Key.SectionName</th>
                 <th>@formSection.Key.SectionDesc</th>
               </tr>
            </thead>
            <tbody>
            @foreach (var question in formSection)
            {
               <tr class="sect-quest-@i">
                 <td>@Html.EditorFor(modelItem => modelItem.SectionQuestion.ElementAt(j).Description)</td>                  
                 <td>@question.Order</td>    
                 <td>@question.Description</td>
               </tr>
                j++;
            }
            </tbody>
            i++;
        } 
        <tfoot>
            <tr>
                <td></td>
                <td></td>
                <td><input type="submit" class="btn btn-primary" value="Edit" /></td>
            </tr>
        </tfoot>
        </table>
    }   @*End Form*@

My Controller FormInfoController.cs

public class FormInfoController : Controller
    {
        ...
        [HttpPost]
        public ActionResult Edit([Bind(Include= "SectionQuestion")] vw_FormInfo vw_forminfo)
        {
            var s = vw_forminfo;
            var sections = vw_forminfo.SectionQuestion;     // This is ALWAYS null
            return View(vw_forminfo);
        }

        ...
    }
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
usr4896260
  • 1,427
  • 3
  • 27
  • 50
  • What does the request look like? (in Chrome -> Dev Tools -> Network tab -> (view your post/xhr)). – Erik Philips Oct 10 '16 at 20:01
  • 1
    Once the page renders, it knows nothing about the model used to build the page. This means to get information from the model to post again, it will need to be somewhere on the page. This is often done with hidden input fields if not all the information is needed on the page but is needed for the post. – Gavin Oct 10 '16 at 20:05
  • You cannot use `foreach` loops (or `.ElementAt()` to generate form controls for collections (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) –  Oct 10 '16 at 20:54

0 Answers0