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
    public Guid Id { get; set; }
    public List&lt;NeonatalDosing&gt; NeoDosing { get; set; }

    public class NeonatalDosing
    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; }
    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; }
    public virtual NeonatalDosing NeonatalDosings { get; set; }
    <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>
    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++)
                        @Html.HiddenFor(m => Model.DrugDosingGuideline[i].xxx)

    Finally, in httppost action:

      public ActionResult Index(VMmodel model)

    More details,you could refer to:


    Best Regards.

    Yuki Tao

    Thursday, November 1, 2018 10:20 AM