MVC3 EditorTemplate使用RadioButtons获得可以为空的布尔值

Nic*_*cht 19 asp.net-mvc mvc-editor-templates asp.net-mvc-3

我在我的一个对象上有一个属性,它是一个可以为空的布尔值,我希望我的逻辑有真正的表示是,假为是否为零,而为零则为零.现在因为我将在许多不同的对象上拥有这样的多个属性,所以最有意义的是为这些属性创建编辑器和显示模板.我将使用jQuery UI来应用buttonset的可视元素,但这一切都有效,但是现在,这超出了我的问题范围.我的编辑器模板看起来像这样.

@model bool?
<div data-ui="buttonset">
@Html.RadioButtonFor(x => x, true, new { id = ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes"}) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))Yes">Yes</label>
@Html.RadioButtonFor(x => x, false, new { id = ViewData.TemplateInfo.GetFullHtmlFieldId("") + "No" }) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))No">No</label>
@Html.RadioButtonFor(x => x, "null", new { id = ViewData.TemplateInfo.GetFullHtmlFieldId("") + "NA" }) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))NA">N/A</label>
</div>
Run Code Online (Sandbox Code Playgroud)

我的问题是,在任何情况下我都无法使用此编辑器模板来正确显示模型的当前值.因为我没有在此范围内呈现模型的属性而是模型本身,所以MVC3中的内置逻辑将不会正确设置checked属性,因为检查是为了验证名称不为空或为null(请参阅MVC3) source,InputExtensions.cs:第#259行).我无法通过与模型进行比较来动态设置checked属性,因为浏览器会检查有关已检查属性是否存在值的单选按钮,因此即使我的单选按钮看起来如下所示,最后一个按钮仍然是选中的.

<div class="span-4" data-ui="buttonset">
<input checked="checked" id="MyObject_BooleanValueYes" name="MyObject.BooleanValue" type="radio" value="True" /><label for="MyObject_BooleanValueYes">Yes</label>
<input checked="" id="MyObject_BooleanValueNo" name="MyObject.BooleanValue" type="radio" value="False" /><label for="MyObject_BooleanValueNo">No</label>
<input checked="" id="MyObject_BooleanValueNA" name="MyObject.BooleanValue" type="radio" value="null" /><label for="MyObject_BooleanValueNA">N/A</label>
</div><span class="field-validation-valid" data-valmsg-for="MyObject.BooleanValue" data-valmsg-replace="true"></span>&nbsp;</div>
Run Code Online (Sandbox Code Playgroud)

我不能有条件地添加HtmlAttribute使用类似的东西,if?truevalue:falsevalue因为真/假部分将是不同的匿名类型,我得到一个错误.

我正在努力应该如何做到这一点,并希望你们中的一个人有关于如何解决这个问题的建议?

Ada*_*gan 27

@model bool?
<div data-ui="buttonset">
@{
    Dictionary<string, object> yesAttrs = new Dictionary<string, object>(); 
    Dictionary<string, object> noAttrs = new Dictionary<string, object>(); 
    Dictionary<string, object> nullAttrs = new Dictionary<string, object>(); 

    yesAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes");
    noAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "No");
    nullAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "NA");

    if (Model.HasValue && Model.Value)
    {
        yesAttrs.Add("checked", "checked");
    }
    else if (Model.HasValue && !Model.Value)
    {
        noAttrs.Add("checked", "checked");
    }
    else
    {
        nullAttrs.Add("checked", "checked");
    }
}

@Html.RadioButtonFor(x => x, "true", yesAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))Yes">Yes</label>
@Html.RadioButtonFor(x => x, "false", noAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))No">No</label>
@Html.RadioButtonFor(x => x, "null", nullAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))NA">N/A</label>
</div>
Run Code Online (Sandbox Code Playgroud)