Let's say I have the following HTML:
<
td>
<%= Html.RadioButton("UsePaybackScheme", true) %>Yes
<%= Html.RadioButton("UsePaybackScheme", false)%>No
</td>
UsePaybackScheme
is a property that appears in my model.
Everything is fine when the view is loaded, the correct value from the model is matched against one of the radio buttons and the correct one is selected/checked. When viewed the source of the HTML generated each checkbox has correct value: one "true" the other "false"
However when the view form is posted and inside the action you do a TryUpdateModel(model) call, and the view is re-displayed (due to other validation errors). Both of the above radio buttons will have the same value, which is the attempted value that you posted. For example, if YES was selected then the value re-rendered for both above radion buttons is "true".
Consequently I looked at the code for Input Checkbox extension. If you look at InputHelper extension (MVC Beta) method in System.Web.Mvc.Html.InputExtensions you notice the lines:
string
attemptedValue = htmlHelper.GetModelAttemptedValue(name);
if (isCheckBox) {
// Helpers that take isChecked as parameter should never look at ViewData
if (useViewData) {
isChecked = htmlHelper.EvalBoolean(name);
}
tagBuilder.MergeAttribute("value", Convert.ToString(value, CultureInfo.CurrentUICulture));
}
else {
tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : Convert.ToString(value,
CultureInfo.CurrentUICulture)));
}
I believe where it is coded: if (isCheckBox) it should instead be: if (isCheckBox || isRadio)
As even the comment in the existing code suggests that for radio buttons it should also behave like checkboxes. As it doesn't make sence for all radio buttons with the same name to use the same attempted value as that would cause problems when the form is re-submited as this time all values are the same and cannot be distinguished.
Am I correct to assume that the above is a bug? That indeed it should be if (isCheckBox || isRadio). Has anyone has a explanation that it should be like it is in the current code and that I should be doing things differently.
Update:
Since the above has been agreed that is a bug. I've built a new version of the MVC with the change above [ changed if (isCheckBox) to if (isCheckBox || isRadio) ]. With this new build I've had no issues (and without any side effects). I'm using this workaround until the next version of the MVC is released.