1

在修补解决[this][1]问题后,我认为问题的核心如下:

当您将 Html.RadioButton() html 助手与 Enum 作为值字段一起使用时,您只能选择一次选项。重新发布页面后,助手将忽略调用中设置的值,并将所有单选按钮设置为相同的值,即您选择的上一个帖子返回的值。难道我做错了什么?

示例(观察按钮的值)

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

会产生

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

并将生成第二次:

<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>
4

3 回答 3

4

这是由于 ASP.NET MVC Beta 代码中的错误造成的。我在 asp.net MVC 论坛上写了对该问题的完整解释。参考这个链接

于 2008-11-10T23:35:03.143 回答
1

万一有人关心这里是一个真正快速而肮脏的工作,以期待框架的下一次更新。它只会用您的值替换值。它没有经过单元测试,也不能保证。

将它放在您的 HtmlHelper 类库中或放置 HtmlHelper 扩展的任何位置。添加以下用法:

  • 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;
    }
    

我知道它可以做得更好等等,但我真的希望它会尽快修复。如果你想让它变得更好,它是社区 wiki,所以继续吧

于 2008-11-12T10:58:27.177 回答
0

一件事要检查。您是否使用更新后的模型来呈现您的视图?即第二次显示时从传递到视图的帖子更新的模型数据是否相同?

于 2008-11-10T11:34:07.607 回答