Please note that the expected behavior of the following code
@Html.RadioButtonFor(x => x.MyEnumProperty, MyEnum.OptionOne)
@Html.RadioButtonFor(x => x.MyEnumProperty, MyEnum.OptionTwo)
@Html.RadioButtonFor(x => x.MyEnumProperty, MyEnum.OptionThree)
is
<input id="MyEnumeration" type="radio" value="Option1"
name="MyEnumeration"
data-val-required="The MyEnumeration field is required." data-val="true">
<input id="MyEnumeration" type="radio" value="Option2" name="MyEnumeration">
<input id="MyEnumeration" type="radio" value="Option3" name="MyEnumeration">
Why? because Html.SomethingFor(model => model.Property)
is meant to generate one html element for one particular property. The way you use it to create more than one element from one property is not correct.
Furthermore, look at the IDs of these 3 radio buttons. They are pretty the same, nah? Is it acceptable to have elements with the same IDs on a html page? Absolutely not! Therefore something needs to be fixed.
Although I thought solving this problem is easier by developing a new Html Extension Methods (reference 2), I tried to solve it in another way (because I'm so stubborn!).
First, We need to change the razor code:
<div>
@Html.RadioButtonFor(m => m.MyEnumeration, MyEnum.Option1, new { id = "m1" })
<label for="m1">
OPTION 1</label>
@Html.RadioButtonFor(m => m.MyEnumeration, MyEnum.Option2, new { id = "m2" })
<label for="m2">
OPTION 2</label>
@Html.RadioButtonFor(m => m.MyEnumeration, MyEnum.Option3, new { id = "m3" })
<label for="m3">
OPTION 3</label>
</div>
Then, a piece of JavaScript/jQuery magic to fix our issue:
<script type="text/javascript">
$(function () {
$('[name=MyEnumeration]').each(function (index) { domAttrModified(this); });
});
function domAttrModified(obj) {
//$obj = $(obj);
$(obj).bind('DOMAttrModified', function () {
if ($(obj).attr('class').indexOf('input-validation-error') != -1)
$(obj).parent().addClass('input-validation-error');
else
$(obj).parent().removeClass('input-validation-error');
});
}
</script>
Anytime the first radio button raises a validation error, this JavaScript code applies error css class (input-validation-error
) to the parent element of radio buttons, which is a <div>
element.
And anytime validation error goes away (by clicking on any of the radio button elements), error style is removed from the parent element.
It works great in IE and FF, but unfortunately doesn't work in Chrome. The reason is Chrome doesn't support DOMAttrModified
event. We can fix it by using this solution: https://mcmap.net/q/412087/-is-there-an-alternative-to-domattrmodified-that-will-work-in-webkit , but maybe sometime later.
Again, I believe we need to develop an HTML Extension Method or improve and use an EditorTemplate like this: https://gist.github.com/973482
References:
- https://mcmap.net/q/153186/-how-do-i-use-html-editorfor-to-render-radio-buttons-in-mvc3
- https://mcmap.net/q/616794/-how-to-make-a-default-editor-template-for-enums
- https://gist.github.com/973482
- Event detect when css property changed using Jquery