views:

2729

answers:

3

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>
A: 

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?

Cristian Libardo
Thanks for your answer. The new one is passed (it works for all my other fields). Although I don't see how this relates to my problem. I will edit my post to make it a bit more clear.
borisCallens
+4  A: 

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

Sia
Thanks. I'm going to check if I can just make an HTML helper method that overrides the framework's bugged one. That way I can still deploy my site to an environment were the regular beta is deployed.
borisCallens
Yeah if you are deploying to an environment with MVC, overriding the helper will be the way to go. In my case I opted for doing a "temp" build of MVC since I'm deploying MVC in the application bin directory and that way when the new MVC release comes I just replace the dll.
Sia
+1  A: 

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

borisCallens