MVC3 EditorTemplate使用单选按钮可为空的布尔 [英] MVC3 EditorTemplate for a nullable boolean using RadioButtons

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

问题描述

我有我的对象是一个可空布尔的一个属性,我希望我的逻辑有真正重新present是的,虚假的是没有和零为N / A。现在,因为我要对许多不同的对象多个属性像这样它使最有意义,使这些属性的编辑和显示模板。我将使用jQuery UI应用在此之后buttonset的视觉元素是所有工作,但现在,这超出了我的问题的范围。我的编辑模板看起来是这样的。

  @model布尔?
< D​​IV数据的UI =buttonset>
@ Html.RadioButtonFor(X => X,真,新{ID = ViewData.TemplateInfo.GetFullHtmlFieldId()+是})<标签=@(ViewData.TemplateInfo.GetFullHtmlFieldId())是>是< /标签>
@ Html.RadioButtonFor(X =&X的催化剂,假的,新的{ID = ViewData.TemplateInfo.GetFullHtmlFieldId()+否})&下;标签=@(ViewData.TemplateInfo.GetFullHtmlFieldId())没有&GT否LT; /标签>
@ Html.RadioButtonFor(X =&X的催化剂,空,新的{ID = ViewData.TemplateInfo.GetFullHtmlFieldId()+NA})&下;标签=@(ViewData.TemplateInfo.GetFullHtmlFieldId( ))NA将N / A< /标签>
< / DIV>

我的问题是,在任何情况下我能得到这个编辑器模板正确显示模型的当前值。因为我不是在这个范围渲染模型的属性,但模型本身,内置的逻辑MVC3将无法正确设置检查属性,因为这是由核实姓名的支票是不为空或空(见MVC3来源InputExtensions.cs:行#259)。我不能动态通过对比示范,因为浏览器检查的检查属性的presence的单选按钮设置checked属性不是它的价值,所以即使我的单选按钮看起来像下面的最后一个仍一个选择。

 < D​​IV CLASS =跨度4数据UI =buttonset>
<输入检查=选中ID =MyObject_BooleanValueYesNAME =MyObject.BooleanValue类型=电台值=真/><标签=MyObject_BooleanValueYes>是< /标签>
<输入检查=ID =MyObject_BooleanValueNoNAME =MyObject.BooleanValue类型=电台值=FALSE/><标签=MyObject_BooleanValueNo>不< /标签>
<输入检查=ID =MyObject_BooleanValueNANAME =MyObject.BooleanValue类型=无线电VALUE =空/><标签=MyObject_BooleanValueNA> N / A< /标签>
< / DIV><跨度类=现场验证,有效的数据valmsg换=MyObject.BooleanValue数据valmsg替换=真正的>< / SPAN>&安培; NBSP;< / DIV>

我不能使用像有条件地添加HtmlAttribute如果truevalue:falsevalue 监守真/假零件会有所不同匿名类型,我得到一个错误。

我挣扎如何应该是这样的,我希望你们中的一个有关于如何解决这个问题有何建议?


解决方案

  @model布尔?
< D​​IV数据的UI =buttonset>
@ {
    字典<字符串对象> yesAttrs =新词典<字符串对象>();
    字典<字符串对象> noAttrs =新词典<字符串对象>();
    字典<字符串对象> nullAttrs =新词典<字符串对象>();    yesAttrs.Add(ID,ViewData.TemplateInfo.GetFullHtmlFieldId()+是);
    noAttrs.Add(ID,ViewData.TemplateInfo.GetFullHtmlFieldId()+否);
    nullAttrs.Add(ID,ViewData.TemplateInfo.GetFullHtmlFieldId()+NA);    如果(Model.HasValue&安培;&安培; Model.Value)
    {
        yesAttrs.Add(选中,检查);
    }
    否则,如果(Model.HasValue&安培;&安培;!Model.Value)
    {
        noAttrs.Add(选中,检查);
    }
    其他
    {
        nullAttrs.Add(选中,检查);
    }
}@ Html.RadioButtonFor(X =&X的催化剂,真,yesAttrs)下;标签=@(ViewData.TemplateInfo.GetFullHtmlFieldId())是>是&下; /标签>
@ Html.RadioButtonFor(X =&X的催化剂,假,noAttrs)下;标签=@(ViewData.TemplateInfo.GetFullHtmlFieldId())否&GT否下; /标签>
@ Html.RadioButtonFor(X => X,空,nullAttrs)LT;标签=@(ViewData.TemplateInfo.GetFullHtmlFieldId())NA将N / A< /标签>
< / DIV>

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使用单选按钮可为空的布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆