MVC3 EditorTemplate 用于使用 RadioButtons 的可为空的布尔值 [英] MVC3 EditorTemplate for a nullable boolean using RadioButtons

查看:25
本文介绍了MVC3 EditorTemplate 用于使用 RadioButtons 的可为空的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个对象上有一个属性,它是一个可为空的布尔值,我希望我的逻辑让 true 表示 Yes,false 表示 No,null 表示 N/A.现在因为我将在许多不同的对象上拥有多个这样的属性,所以为这些属性制作一个编辑器和显示模板是最有意义的.我将使用 jQuery UI 来应用 buttonset 的视觉元素,但现在,这超出了我的问题范围.我的编辑器模板如下所示.

@model bool?<div data-ui="buttonset">@Html.RadioButtonFor(x => x, true, new { id = ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes"}) 

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

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

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

我正在苦苦思索如何实现这一点,希望你们中的一个人对如何解决这个问题提出建议?

解决方案

@model bool?<div data-ui="buttonset">@{字典<字符串,对象>yesAttrs = new Dictionary();字典<字符串,对象>noAttrs = new Dictionary();字典<字符串,对象>nullAttrs = new Dictionary();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");}否则如果 (Model.HasValue && !Model.Value){noAttrs.Add("checked", "checked");}别的{nullAttrs.Add("checked", "checked");}}@Html.RadioButtonFor(x => x, "true", yesAttrs) @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>

I have a property on one of my objects that is a nullable boolean, I want my logic to have true represent Yes, false to be No and null to be N/A. Now because I am going to have multiple properties like this on many different objects it makes the most sense to make an editor and display templates for these properties. I am going to use jQuery UI to apply a visual element of buttonset after this is all working but for now, that's beyond the scope of my problem. My editor template looks like this.

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

My problem is that under no circumstances can I get this editor template to show the current value of the model correctly. Because I am not rendering a property of a model at this scope but the model itself, the built in logic in MVC3 will not set the checked property correctly because of a check that is made to verify the name is not empty or null (See MVC3 source, InputExtensions.cs:line#259). I can't set the checked attribute dynamically by comparing to the Model because the browser checks the radiobutton on the presence of the checked attribute not it's value, so even though my radio buttons would look like the following the last one is still the one selected.

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

I can't conditionally add the HtmlAttribute using something like if?truevalue:falsevalue becuase the true/false parts would be of different anonymous types and I get an error.

I'm struggling on how this should be done and am hoping one of you have a suggestion on how to tackle this problem?

解决方案

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

这篇关于MVC3 EditorTemplate 用于使用 RadioButtons 的可为空的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆