locked
Modelbinding question List within a List is null RRS feed

  • Question

  • User657329123 posted

    <p>Hi there,</p>
    <p>Here are my models</p>
    <pre class="prettyprint lang-auto">public class DrugDosingGuideline
    {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public List&lt;NeonatalDosing&gt; NeoDosing { get; set; }
    }

    public class NeonatalDosing
    {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }<br> public string DosingCriteria { get; set; }
    [Display(Name = &quot;Neonatal Maximum&quot;)]
    public List&lt;DrugDosingNeoMaximum&gt; NeoMax { get; set; }
    public Guid DrugDosingGuideline_id { get; set; }
    [ForeignKey(&quot;DrugDosingGuideline_id&quot;)]
    public virtual DrugDosingGuideline DrugDosingGuidelines { get; set; }
    }

    public class DrugDosingNeoMaximum
    {
    public int Id { get; set; }
    public string Amount { get; set; }
    public Guid NeonatalDosing_id { get; set; }
    [ForeignKey(&quot;NeonatalDosing_id&quot;)]
    public virtual NeonatalDosing NeonatalDosings { get; set; }
    }</pre>
    <p>In my form, I have a button that allows users to add one or more NeonatalDosing sections. Within NeonatalDosing section, there is a button NeoMaximum which allows user to add one or more DrugDosingNeoMaximum sections.</p>
    <p>When I submit the form, I can see the values for NeonatalDosing but DrugDosingNeoMaximum is always null for some reason. This is what browser shows</p>
    <pre class="prettyprint lang-auto">&lt;input id=&quot;NeoDosing_0_DosingCriteria&quot; name=&quot;NeoDosing[0].DosingCriteria&quot; type=&quot;text&quot; value=&quot;&quot; class=&quot;form-control&quot;&gt;

    &lt;input id=&quot;NeonatalDosing_0_NeoMax_0_Amount&quot; name=&quot;NeonatalDosing[0].NeoMax[0].Amount&quot; type=&quot;text&quot; value=&quot;&quot; class=&quot;form-control&quot;&gt;<br><br></pre>
    <p>It seems that something is wrong with &lt;input id=&quot;NeonatalDosing_0_NeoMax_0_Amount&quot; name=&quot;NeonatalDosing[0].NeoMax[0].Amount&quot; type=&quot;text&quot; value=&quot;&quot; class=&quot;form-control&quot;&gt;. What will be the correct id and name to refer to Amount that is within DrugDosingNeoMaximum
    which is within NeonatalDosing?</p>
    <p>Joe</p>
    Wednesday, October 31, 2018 5:54 PM

All replies

  • User1520731567 posted

    Hi joegreen2005,

    The most critical thing about using Model binding is type matching between controller and view.

    You should pay attention to the model type from httpget action to View ,and from View to httppost action.

    According to this part of code:

    <input id="NeoDosing_0_DosingCriteria" name="NeoDosing[0].DosingCriteria" type="text" value="" class="form-control">
    
    <input id="NeonatalDosing_0_NeoMax_0_Amount" name="NeonatalDosing[0].NeoMax[0].Amount" type="text" value="" class="form-control">

    It seems that you use multiple models in your view.

    I suggest you could use ViewModel to assemble the models you need.

    For example:

             public class VMmodel
            {
                public DrugDosingGuideline DrugDosingGuideline { get; set; }
                public List<DrugDosingGuideline> DrugDosingGuidelinelst { get; set; }// List<> or not depend on your need
    
                public NeonatalDosing NeonatalDosing { get; set; }
                public List<NeonatalDosing> NeonatalDosinglst { get; set; }
    
                public DrugDosingNeoMaximum DrugDosingNeoMaximum { get; set; }
                public List<DrugDosingNeoMaximum> DrugDosingNeoMaximumlst { get; set; }
            }

    And the in httpget action ,return this VMmodel.

    Quote this VMmodel which retruned by httpget action In View:

    @model xxx.Controllers.Model.VMmodel
    ...
    
    
     @Html.EditorFor(m => Model.DrugDosingGuideline.xxx)
    
    
     @for (int i = 0; i < Model.DrugDosingGuidelinelst.Count; i++)
            {
                <tr>
                   
                    <td>
                        @Html.HiddenFor(m => Model.DrugDosingGuideline[i].xxx)
    
                    </td>
                </tr>
            }
    ...

    Finally, in httppost action:

     [HttpPost]
      public ActionResult Index(VMmodel model)
    {
    }

    More details,you could refer to:

    https://stackoverflow.com/questions/26155607/mvc-post-a-list-of-complex-objects

    Best Regards.

    Yuki Tao

    Thursday, November 1, 2018 10:20 AM