在MVC 2.0里支持强类型实体绑定,可以直接使用如 <%: Html.TextBoxFor(model => model.Description, new { @class="text"})%>来自动将实体绑定到表单里,但我不知道如何使用 Html.RadioButtonFor 来生成 RadioButton ? 在使用 Html.RadioButtonFor 必须至少2个参数,另一个是指一个object,我绑定的字段是一个bool型的,如何能自动生成相关的RadioButton 呢?谢啦!!

解决方案 »

  1.   

    唉,还是我自己找到了解决的办法了,之前没想到要一条条语句来生成的,其实也很简单,呵呵,在此也顺便写出来让需要的人看看吧:
    <%
                                     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就可以了
      

  2.   

    呃,MVC还没怎么学。顶一个、感觉和三元运算有点像、
      

  3.   

    其中参数 true 或false在传的时候有点麻烦,有时候还得写额外的代码来返回true或false
    就像这样:
    <%: Html.RadioButtonFor(model => model.OffStatus, model.OffStatus==1?true:false, new { @id="radio1", @name = "Status" })%>感觉这点mvc应该再改进的更方便点
      

  4.   

    不需要写任何额外的代码来返回true或false
    直接可以这样使用:
    <%=Html.RadioButtonFor(model => model.OffStatus,"开启", new { @id="radio1", @name = "Status" })%>
    如果model.OffStatus值为"开启",那么它会自动选中
      

  5.   

    这位兄弟说的对。当model.OffStatus的值和value参数的值相等时,自然会被选中
      

  6.   

    MVC3中 可以这样         
       <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>