asp.net mvc - MVC3 EditorTemplate for a nullable boolean using RadioButtons -
i have property on 1 of objects nullable boolean, want logic have true represent yes, false no , null n/a. because going have multiple properties on many different objects makes sense make editor , display templates these properties. going use jquery ui apply visual element of buttonset after working now, that's beyond scope of problem. editor template looks 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 under no circumstances can editor template show current value of model correctly. because not rendering property of model @ scope model itself, built in logic in mvc3 not set checked property correctly because of check made verify name not empty or null (see mvc3 source, inputextensions.cs:line#259). can't set checked attribute dynamically comparing model because browser checks radiobutton on presence of checked attribute not it's value, though radio buttons following last 1 still 1 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> </div>
i can't conditionally add htmlattribute using if?truevalue:falsevalue
becuase true/false parts of different anonymous types , error.
i'm struggling on how should done , hoping 1 of have suggestion on how tackle 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>
Comments
Post a Comment