在MVC 2.0里支持强类型实体绑定,可以直接使用如 <%: Html.TextBoxFor(model => model.Description, new { @class="text"})%>来自动将实体绑定到表单里,但我不知道如何使用 Html.RadioButtonFor 来生成 RadioButton ? 在使用 Html.RadioButtonFor 必须至少2个参数,另一个是指一个object,我绑定的字段是一个bool型的,如何能自动生成相关的RadioButton 呢?谢啦!!
<%
if(Model.OffStatus){ %>
<%: Html.RadioButtonFor(model => model.OffStatus, true, new { @id="radio1", @name = "Status" })%>
<label for="radio1">开启</label>
<%: Html.RadioButtonFor(model => model.OffStatus, false, new { @id = "radio2", @name = "Status" })%>
<label for="radio2">关闭</label>
<%}else { %>
<%: Html.RadioButtonFor(model => model.OffStatus, false, new { @id = "radio1", @name = "Status" })%>
<label for="radio1">开启</label>
<%: Html.RadioButtonFor(model => model.OffStatus, true, new { @id = "radio2", @name = "Status" })%>
<label for="radio2">关闭</label>
<%} %>
只要分别进行判断然后显示不同的radiobutton就可以了
就像这样:
<%: Html.RadioButtonFor(model => model.OffStatus, model.OffStatus==1?true:false, new { @id="radio1", @name = "Status" })%>感觉这点mvc应该再改进的更方便点
直接可以这样使用:
<%=Html.RadioButtonFor(model => model.OffStatus,"开启", new { @id="radio1", @name = "Status" })%>
如果model.OffStatus值为"开启",那么它会自动选中
<div class="radios">
@Html.RadioButtonFor(model => model.Activity, true, new { @id = "radio3", @name = "Activity" })<label id="tr">启用</label>
@Html.RadioButtonFor(model => model.Activity, false, new { @id = "radio4", @name = "Activity" })<label id="fl">禁用</label>
</div>