Asp.net mvc 2 如何将ASp.net MVC 2中的RadioButtonNforce()设置为默认选中状态

Asp.net mvc 2 如何将ASp.net MVC 2中的RadioButtonNforce()设置为默认选中状态,asp.net-mvc-2,radiobuttonfor,Asp.net Mvc 2,Radiobuttonfor,如何将RadioButtonOn()设置为默认选中状态 <%=Html.RadioButtonFor(m => m.Gender,"Male")%> m.性别,“男性”)%> (Html.RadioButton)有出路,但(Html.RadioButton)没有出路 有什么想法吗?我想你应该有一组单选按钮。事情可能是这样的 <%=Html.RadioButtonFor(m => m.Gender,"Male")%> <%=Html.RadioButt

如何将RadioButtonOn()设置为默认选中状态

<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
m.性别,“男性”)%>
(Html.RadioButton)有出路,但(Html.RadioButton)没有出路


有什么想法吗?

我想你应该有一组单选按钮。事情可能是这样的

<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
<%=Html.RadioButtonFor(m => m.Gender,"Female")%>
<%=Html.RadioButtonFor(m => m.Gender,"Unknown")%>
m.性别,“男性”)%>
m、 性别,“女性”)%>
m、 性别,“未知”)%%>

您可以从控制器中为m.Gender=“Unknown”(或其他内容)提供默认值。

处理RadioButtonListFor,答案也解决了您的问题。您可以在RadioButtonListViewModel中设置所选属性。

如果使用jquery,可以在单选按钮之前调用此属性

$('input:radio:first').attr('checked', true);

^这将选中第一个单选框,但您可以查看更多jquery以循环选择您想要选择的单选框。

这并不太漂亮,但是如果您必须为整个站点只实现很少的单选按钮,类似这样的选项也可能是一个选项:

m.性别,“男性”,模特。性别==“男性”?新建{@checked=“checked”}:null)%>

使用以下简单方法:

<%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%>
m.性别,“男性”,新的{Checked=“Checked”})%>

此帮助程序计算表达式,如果等于该值,则检查单选按钮,并具有与RadioButtonOn相同的参数(因此名称不同):

公共静态MvcHtmlString CheckedRadioButtonFor(此HtmlHelper HtmlHelper、表达式、对象值)
{
返回CheckedRadioButtonFor(htmlHelper,表达式,值,null);
}
公共静态MvcHtmlString CheckedRadioButtonFor(此HtmlHelper HtmlHelper、表达式、对象值、对象htmlAttributes)
{
var func=expression.Compile();
var属性=新的RouteValueDictionary(htmlAttributes);
if((对象)func(htmlHelper.ViewData.Model)=值){
属性[“选中”]=“选中”;
}
返回htmlHelper.RadioButton(表达式、值、属性);
}
用法:

<%= Html.CheckedRadioButtonFor(m => m.Gender, "Male", new { id = "gender-male" })%>
m.性别,“男性”,新的{id=“性别男性”})%>
结果:

<!-- For Model.Gender = "Male" -->
<input checked="checked" id="gender-male" name="Gender" type="radio" value="Male">
<!-- For Model.Gender = "Female" -->
<input id="gender-male" name="Gender" type="radio" value="Male">

您还可以添加绑定到具有相同ID的单选按钮的标签,这样用户就可以单击单选按钮或标签来选择该项目。我在这里使用常量表示“男性”、“女性”和“未知”,但很明显,这些可能是模型中的字符串

<%: Html.RadioButtonFor(m => m.Gender, "Male", 
    new Dictionary<string, object> { { "checked", "checked" }, { "id", "Male" } }) %>
<%: Html.Label("Male") %>

<%: Html.RadioButtonFor(m => m.Gender, "Female", 
    new Dictionary<string, object> { { "id", "Female" } }) %>
<%: Html.Label("Female")%>

<%: Html.RadioButtonFor(m => m.Gender, "Unknown",
    new Dictionary<string, object> { { "id", "Unknown" } }) %>
<%: Html.Label("Unknown")%>
m.性别,“男性”,
新字典{{“checked”,“checked”},{“id”,“Male”})%>
m、 性别,“女性”,
新字典{{“id”,“Female”})%>
m、 性别,“未知”,
新字典{{“id”,“未知”})%>

如果您喜欢

我找到了另一个选项,因此您可以将@Html.EditorFor()与模板一起使用:

假设我有这个枚举:

public enum EmailType { Pdf, Html }
我可以将此代码放在视图/Shared/EditorTemplates/EmailType.cshtml中

@model EmailType
@{
    var htmlOptions = Model == EmailType.Html ? new { @checked = "checked" } : null;
    var pdfOptions = Model == EmailType.Pdf ? new { @checked = "checked" } : null;
}

@Html.RadioButtonFor(x => x, EmailType.Html, htmlOptions) @EmailType.Html.ToString()
@Html.RadioButtonFor(x => x, EmailType.Pdf, pdfOptions) @EmailType.Pdf.ToString()
现在,如果我想在任何时候使用它,我可以简单地使用它:

@Html.EditorFor(x => x.EmailType)

这种方式更通用,我觉得更容易更改。

下面是将默认单选按钮设置为true的代码

           @Html.RadioButton("Insured.GenderType", 1, (Model.Insured.GenderType == 1 ))
           @Web.Mvc.Claims.Resources.PartyResource.MaleLabel
           @Html.RadioButton("Insured.GenderType", 2, Model.Insured.GenderType == 2)
           @Web.Mvc.Claims.Resources.PartyResource.FemaleLabel
@Html.radioButton(m=>m.Gender,“男性”,新的{@checked=“checked”,id=“rdGender”,name=“rbGender”})

我发现最好将默认值放在模型的构造函数方法中

Gender = "Male";

如果radiobutton的值与Model.Gender值匹配,则需要在radiobutton中为添加“checked”htmlAttribute

@{
        foreach (var item in Model.GenderList)
        {
            <div class="btn-group" role="group">
               <label class="btn btn-default">
                   @Html.RadioButtonFor(m => m.Gender, item.Key, (int)Model.Gender==item.Key ? new { @checked = "checked" } : null)
                   @item.Value
              </label>
            </div>
        }
    }
@{
foreach(Model.GenderList中的var项)
{
@RadioButtonOn(m=>m.Gender,item.Key,(int)Model.Gender==item.Key?新建{@checked=“checked”}:null)
@项目.价值
}
}
有关完整的代码,请参见下面的链接:在选中默认值的情况下呈现引导单选按钮组。
遇到这个问题,我想我会指出,对于MVC 5,您需要做的就是在模型上设置值。例如:

型号:

   public class ExampleModel
    {
        public PackingListInputModel()
        {

             RadioButtonField = "One";

        }
        public string RadioButtonField { get; set; }
    }
视图:

@model ExampleModel

@using (Html.BeginForm)
{
    @Html.RadioButtonFor(m => m.RadioButtonField , "One")
    @Html.RadioButtonFor(m => m.RadioButtonField , "Two")
}

第一个单选按钮(“一”)的状态将设置为活动状态,因为该值与模型中设置的值匹配

使用radiobutton()时没有默认检查选项,但我只需要两个radiobutton,其中一个是使用Html默认检查的。radiobutton()我也很难做到这一点,但发现这个答案很简单。如果在模型的默认构造函数中创建了一个属性,比如说Gender,只需将该属性设置为默认Gender,比如
Gender=“Male”。如果您这样做,mvc.net将自动选择默认值,并且在回发时将自动将所选值绑定到您的性别属性。谢谢!也可以是
new{@checked=“checked”}
。我可以在这里将checked属性绑定到模型的布尔属性吗?这样,当我单击它时,布尔属性变为true,我可以将该true值绑定到动作方法?谢谢!在为复选框创建标签时遇到困难。现在已经很明显了,但是我想不出来!您应该使用
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
而不是
新的RouteValueDictionary(htmlAttributes)
以避免丢失将
数据\u某物
自动转换为
数据某物
@{
        foreach (var item in Model.GenderList)
        {
            <div class="btn-group" role="group">
               <label class="btn btn-default">
                   @Html.RadioButtonFor(m => m.Gender, item.Key, (int)Model.Gender==item.Key ? new { @checked = "checked" } : null)
                   @item.Value
              </label>
            </div>
        }
    }
   public class ExampleModel
    {
        public PackingListInputModel()
        {

             RadioButtonField = "One";

        }
        public string RadioButtonField { get; set; }
    }
@model ExampleModel

@using (Html.BeginForm)
{
    @Html.RadioButtonFor(m => m.RadioButtonField , "One")
    @Html.RadioButtonFor(m => m.RadioButtonField , "Two")
}