mercredi 31 janvier 2018

Passing multiple checkbox values to deeper level of model .net mvc

A few similar questions have been asked before but my use case is a bit different.

So this is my model:

  public class YourModel
    {
        public string[] Suburb { get; set; }
    }

And my view:

    <input name="Suburb" type="checkbox" value="sydney" /><span>sydney</span>
    <input name="Suburb" type="checkbox" value="melbourne" /><span>melbourne</span>

Controller:

 public ActionResult AdvancedSearch(YourModel s)
        {
            //logic
        }

So MVC is smart enough to reteive the multiple checkbox values to put them in the Suburb array in YourModel model. I can inspect all values there. But my use case is that the YourModel is just the nested model inside another model MyModel:

  public class MyModel
    {
        //other properties
        public YourModel m { get; set; }
    }

So now How do I make MVC post the checkbox values to a deeper model MyModel.YourModel ? I have tried @Html.CheckBoxFor and @Html.CheckBox but neither of them worked.

Right now my work around is to add a temporary array placeholder in the outside model and then assign all the data to the inside model when available, but that is definitely not ideal.




Aucun commentaire:

Enregistrer un commentaire