Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 多布尔单选按钮_Asp.net_Asp.net Mvc - Fatal编程技术网

Asp.net 多布尔单选按钮

Asp.net 多布尔单选按钮,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,假设我的模型中有以下属性,我希望它们相互排斥: public bool PrintWeek1 {get; set;} public bool PrintWeek2 {get; set;} public bool PrintWeek3 {get; set;} 是否可以将它们呈现为一组单选按钮,或者是否需要将它们更改为枚举 如果我使用@Html.radiobutton for它会将name呈现为属性的名称,因此它们不能正确分组。这里有一个快速解决方案,让您在模型中拥有以下属性- public b

假设我的模型中有以下属性,我希望它们相互排斥:

public bool PrintWeek1 {get; set;}
public bool PrintWeek2 {get; set;}
public bool PrintWeek3 {get; set;} 
是否可以将它们呈现为一组单选按钮,或者是否需要将它们更改为枚举


如果我使用
@Html.radiobutton for
它会将name呈现为属性的名称,因此它们不能正确分组。

这里有一个快速解决方案,让您在模型中拥有以下属性-

public bool PrintWeek1 { get; set; }
public bool PrintWeek2 { get; set; }
public bool PrintWeek3 { get; set; }
public string SelectedValue { get; set; }
那么你的HTML应该是这样的-

@Html.RadioButtonFor(Model => Model.PrintWeek1, "PrintWeek1", new { @Name = "SelectedValue" }) 
@Html.RadioButtonFor(Model => Model.PrintWeek2, "PrintWeek2", new { @Name = "SelectedValue" }) 
@Html.RadioButtonFor(Model => Model.PrintWeek3, "PrintWeek3", new { @Name = "SelectedValue" })
然后,当您提交表单时,您将在
SelectedValue
属性中获得所选值

编辑 为解决@StephenMuecke point问题,创建了以下解决方案-

创建一个
枚举
-

public enum PrintWeekType
{
    PrintWeek1, PrintWeek2, PrintWeek3
}
然后有一个模型特性(而不是单个特性,有单个emum特性)——

HTML应该如下所示-

@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek1) 
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek2) 
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek3)

使用上述示例,您可以预先选择一个单选按钮,同时我们可以在
SelectedValue
属性中发布所选值。

下面是一个快速解决方案,让您在模型中具有以下属性-

public bool PrintWeek1 { get; set; }
public bool PrintWeek2 { get; set; }
public bool PrintWeek3 { get; set; }
public string SelectedValue { get; set; }
那么你的HTML应该是这样的-

@Html.RadioButtonFor(Model => Model.PrintWeek1, "PrintWeek1", new { @Name = "SelectedValue" }) 
@Html.RadioButtonFor(Model => Model.PrintWeek2, "PrintWeek2", new { @Name = "SelectedValue" }) 
@Html.RadioButtonFor(Model => Model.PrintWeek3, "PrintWeek3", new { @Name = "SelectedValue" })
然后,当您提交表单时,您将在
SelectedValue
属性中获得所选值

编辑 为解决@StephenMuecke point问题,创建了以下解决方案-

创建一个
枚举
-

public enum PrintWeekType
{
    PrintWeek1, PrintWeek2, PrintWeek3
}
然后有一个模型特性(而不是单个特性,有单个emum特性)——

HTML应该如下所示-

@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek1) 
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek2) 
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek3)

使用上面的示例,可以预先选择单选按钮,同时我们可以在
SelectedValue
属性中发布所选值。

好的,我放弃了bools,只是使用了一个列表-这似乎是最快捷、最简单的方法

我在其中初始化模型:

   public PrintViewModel()
        {
            this.PrintTypes = new List<string>() { "Print Week 1", "Print Week 2", "Print Week 3" };
        }

    public List<string> PrintTypes { get; set; }
    public string SelectedPrintType { get; set; }
publicprintviewmodel()
{
this.PrintTypes=新列表(){“打印第1周”、“打印第2周”、“打印第3周”};
}
公共列表打印类型{get;set;}
公共字符串SelectedPrintType{get;set;}
在我看来(我希望默认选择第一个选项):

@for(int i=0;ix.SelectedPrintType,Model.PrintTypes[i],新的{@checked=“checked”}):@Html.RadioButton(x=>x.SelectedPrintType,Model.PrintTypes[i]))
@模型.打印类型[i]
}

好吧,我放弃了bools,只是使用了一个列表——这似乎是最快捷、最简单的方法

我在其中初始化模型:

   public PrintViewModel()
        {
            this.PrintTypes = new List<string>() { "Print Week 1", "Print Week 2", "Print Week 3" };
        }

    public List<string> PrintTypes { get; set; }
    public string SelectedPrintType { get; set; }
publicprintviewmodel()
{
this.PrintTypes=新列表(){“打印第1周”、“打印第2周”、“打印第3周”};
}
公共列表打印类型{get;set;}
公共字符串SelectedPrintType{get;set;}
在我看来(我希望默认选择第一个选项):

@for(int i=0;ix.SelectedPrintType,Model.PrintTypes[i],新的{@checked=“checked”}):@Html.RadioButton(x=>x.SelectedPrintType,Model.PrintTypes[i]))
@模型.打印类型[i]
}

您可以指定自己的名称-
新建{@name=“grp”}
,以便将它们分组在一起。尝试使用单个枚举属性(具有3个值和3个相应的单选按钮)将更有效sense@ramiramilu啊,我试着使用
new{@name=“grp”}
,它没有覆盖MVC正在呈现的名称。我现在会检查他们是否正确发回。实际上,这有助于相互排斥,但更改名称实际上会在发布模型时产生问题。您实际上是如何将数据发回服务器的?相反,我建议您使用
RadioButtonListFor
,如图所示-您可以指定自己的名称-
new{@name=“grp”}
,以便将它们分组在一起。尝试此操作,单个枚举属性(具有3个值和3个相应的单选按钮)将更有效sense@ramiramilu啊,我试着用
new{@name=“grp”}
它没有覆盖MVC正在呈现的名称。我现在会检查他们是否正确发回。实际上,这有助于相互排斥,但更改名称实际上会在发布模型时产生问题。您实际上是如何将数据发回服务器的?相反,我建议您使用
RadioButtonListFor
,如图所示-这不起作用,并提供双向模型绑定。首次渲染视图时,无论
布尔值是多少,都不会选择任何单选按钮properties@StephenMuecke你是对的,用更精确的解决方案更新了我的答案:-)这不起作用,并给出了双向模型绑定。首次渲染视图时,无论
布尔值是多少,都不会选择任何单选按钮properties@StephenMuecke你说得对,用更精确的答案更新了我的答案:-)这很有效。谢谢这起作用了。谢谢