Question

After tinkering around to solve [this][1] problem, I think the core of the problem is the following:

When you use the Html.RadioButton() html helper with an Enum as value field, you can only choose your option once. AFter reposting the page, the helpers will ignore the value set in the call and set all radio buttons to the same value, being the value you selected the previous post back. Am I doing something wrong?

Example (watch the value of the buttons)

<fieldset>
    <legend>Test</legend>                            
    <div>
        <label for="SearchBag.EffectIndicatorAny" id="EffectIndicatorAnyLabel">
            Any
        </label>                
        <%=Html.RadioButton("SearchBag.EffectIndicator", "Any" , ViewData.Model.SearchBag.EffectIndicatorIsAny, new { @id = "SearchBag.EffectIndicatorAny" })%>
    </div>
    <div>
        <label for="SearchBag.EffectIndicatorSolid" id="EffectIndicatorSolidLabel">
            Solid
        </label>                
        <%=Html.RadioButton("SearchBag.EffectIndicator", "Solid", ViewData.Model.SearchBag.EffectIndicatorIsSolid, new { @id = "SearchBag.EffectIndicatorSolid" })%>
    </div>
    <div>
        <label for="SearchBag.EffectIndicatorEffect" id="EffectIndicatorEffectLabel">
            Effect
        </label>                
        <%=Html.RadioButton("SearchBag.EffectIndicator", "Effect", ViewData.Model.SearchBag.EffectIndicatorIsEffect, new { @id = "SearchBag.EffectIndicatorEffect" })%>
    </div>
</fieldset>

Will generate

<fieldset>
    <legend>Effect</legend>                            
    <div class="horizontalRadio">
        <label for="SearchBag.EffectIndicatorAny" id="EffectIndicatorAnyLabel">
            Any                                      
        </label>                
        <input checked="checked" id="SearchBag.EffectIndicatorAny" name="SearchBag.EffectIndicator" type="radio" value="Any" />
    </div>
    <div class="horizontalRadio">
        <label for="SearchBag.EffectIndicatorSolid" id="EffectIndicatorSolidLabel">
            Solid
        </label>                
        <input id="SearchBag.EffectIndicatorSolid" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
    </div>
    <div class="horizontalRadio">
        <label for="SearchBag.EffectIndicatorEffect" id="EffectIndicatorEffectLabel">
            Effect
        </label>                
        <input id="SearchBag.EffectIndicatorEffect" name="SearchBag.EffectIndicator" type="radio" value="Effect" />
    </div>
</fieldset>

And will generate the second time:

<fieldset>
    <legend>Effect</legend>                            
    <div class="horizontalRadio">
        <label for="SearchBag.EffectIndicatorAny" id="EffectIndicatorAnyLabel">
            Any                                      
        </label>                
        <input id="SearchBag.EffectIndicatorAny" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
    </div>
    <div class="horizontalRadio">
        <label for="SearchBag.EffectIndicatorSolid" id="EffectIndicatorSolidLabel">
            Solid
        </label>                
        <input checked="checked" id="SearchBag.EffectIndicatorSolid" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
    </div>
    <div class="horizontalRadio">
        <label for="SearchBag.EffectIndicatorEffect" id="EffectIndicatorEffectLabel">
            Effect
        </label>                
        <input id="SearchBag.EffectIndicatorEffect" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
    </div>
</fieldset>
Was it helpful?

Solution

This is due to a bug in the ASP.NET MVC Beta code. I wrote a full explanation of the issue at asp.net MVC forum. Refer to this link

OTHER TIPS

In case anybody cares here is a real quick and dirty work around in anticipation for the next update of the framework. It only regexreplaces the value with your value. It's not unit tested, not guaranteed not whatever.

Put it in your HtmlHelper class library or wherever you put HtmlHelper extentions. add the following usings:

  • System.Text.RegularExpressions;
  • System.Web.Mvc.Html;

    /*ToDo: remove when patched in framework*/
    public static string MonkeyPatchedRadio(this HtmlHelper htmlHelper, string name, object value, bool isChecked, object htmlAttributes){
        string monkeyString = htmlHelper.RadioButton(name, value, isChecked, htmlAttributes);
        monkeyString = Regex.Replace(monkeyString, "(?<=value=\").*(?=\".*)", value.ToString());            
        return monkeyString;
    }
    

I know it can be done better and whatnot, but I really hope it will be fixed soon anyway. If you feel like making it better, it's community wiki, so go ahead

One thing to check. Are you using the updated model to render your view? I.e. is the same model data that was updated from the post passed to the view the second time it's displayed?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top