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.

2
  • 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 for an example Commented Jan 31, 2018 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) Commented Jan 31, 2018 at 11:38

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

8 Comments

I think you mean name="m.Suburb" based on OP's code
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 It should be name="m.Suburb" as Stephen mentions. Your model doesn't contain a property named MyModel
@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.
Not sure why people upvote this incorrect answer. Upvoters: try yourself, this answer is wrong.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.