0

When I set custom name attribute in HTML Helper (in this case it's a list of checkbox) using new { @Name = "MyName" } I'm not getting true for any of the checkbox if I select then form UI on post.

But if I remove the name attribute from HTML Helper, it's working fine.

Any suggestion would be really helpful.

asp.net mvc: why is Html.CheckBox generating an additional hidden input

The above link gives me a fair idea about why this is not working if I add the name attribute. But in that case how should I achieve validation such that at least one checkbox from the group must be selected?

Community
  • 1
  • 1
Krishnandu Sarkar
  • 494
  • 1
  • 6
  • 21
  • You have not posted enough code to be sure, but there is nothing out of the box that will do this. Simple solution is to check on the controller and then add a `ModelState` error and return the view. Complex solution is to create you own validation attribute. –  Dec 18 '14 at 22:23
  • Assuming you use a model for this, if you change the name of your checkbox mvc will not be able to map the posted value back to you model. If your model value is named "MyCheckBox" but you rename it to "MyName", the framework will not repopulate you MyCheckBox property. To make your checkbox show a different name use a [DisplayNameAttribute](http://msdn.microsoft.com/de-de/library/system.componentmodel.displaynameattribute(v=vs.110).aspx) – svanelten Dec 19 '14 at 09:48
  • Well svanelten you got it till some point. What I meant is, yes I'm using Model to bind View. And I have a List in my model which I'm binding in view using CheckBoxFor. Now I want to do validation on post that one of the checkbox from the list should be selected. – Krishnandu Sarkar Dec 19 '14 at 10:54

0 Answers0