2

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 retrieve 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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Frostless
  • 818
  • 1
  • 11
  • 33
  • What your should be doing in that case is to use a view model(s) which will include a `bool IsSelected` property to bind to - refer [this answer](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Jan 31 '18 at 11:35
  • Even if you used `name="m.Suburb"` to bind in the POST method, you would lose all model binding in the view if you need to return the view because `ModelState` is invalid, or if you need to edit existing data (previous selections will be lost and you will not get any client side validation) –  Jan 31 '18 at 11:38

2 Answers2

3

You need to use add MyModel

<input name="m.Suburb" type="checkbox" value="sydney" /><span>sydney</span>
<input name="m.Suburb" type="checkbox" value="melbourne" /><span>melbourne</span>
programtreasures
  • 4,250
  • 1
  • 10
  • 29
  • 2
    I think you mean `name="m.Suburb"` based on OP's code –  Jan 31 '18 at 11:36
  • I tried that. But I used MyModel.m.Surburd (because MyModel does not have the Suburb property) and I get a System.NullReferenceException: which is expected, because when you get Suburb it is null. Do I miss something here? – Frostless Jan 31 '18 at 11:38
  • @Frostless It should be `name="m.Suburb"` as Stephen mentions. Your model doesn't contain a property named `MyModel` – Camilo Terevinto Jan 31 '18 at 11:40
  • @Camilo Terevinto that worked, you just saved my day. Actually what I used before was `@Model.m.Suburb` and it gave me the `NullReferenceException` error. A bit surprise that `name="m.Suburb" ` does not give me that. – Frostless Jan 31 '18 at 11:49
  • Not sure why people upvote this incorrect answer. Upvoters: try yourself, this answer is wrong. – Camilo Terevinto Jan 31 '18 at 11:53
  • @CamiloTerevinto, Your answer is exactly the same (and they are both awful solutions) –  Jan 31 '18 at 11:56
  • @StephenMuecke Now, after over 20 minutes it was edited into a correct answer, it wasn't the same when I posted mine. Not sure why it would be awful, though – Camilo Terevinto Jan 31 '18 at 11:57
  • @CamiloTerevinto, Read my comments in the question –  Jan 31 '18 at 11:58
0

In Razor, you don't have to define the name of the top-most Model, only the names of the properties of inner models:

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

However, I'd strongly suggest you to change that m name to something more significant.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120