Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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
C# 如何区分单选按钮?_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 如何区分单选按钮?

C# 如何区分单选按钮?,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个for循环,每次迭代都有一个单选按钮。我的问题是,一旦我验证了我的表单(如何检索它们的值?),我就不知道如何区分每个单选按钮。我有一个打印机模型: public class Printer{ public int id{ get; get;} public string printerName { get; set; } } 它本身是另一个模型的一部分: public class FinalSiteModel { public string name{ g

我有一个for循环,每次迭代都有一个
单选按钮。我的问题是,一旦我验证了我的表单(如何检索它们的值?),我就不知道如何区分每个
单选按钮。我有一个打印机模型:

public class Printer{
     public int id{ get; get;}
     public string printerName { get; set; }
}
它本身是另一个模型的一部分:

public class FinalSiteModel 
{
    public string name{ get; set; }
    public List<Printer> printers{ get; set; }
    public bool selected { get; set; }
}
这一位:
m=>m.printers[i]。选中的
为每个单选按钮创建了一个不同的名称,因此当我同时检查多个单选按钮时,我不希望这样做


我猜我应该为
RadioButton添加一个参数

您的单选按钮需要一个值来绑定到所选的
属性。但是,该属性必须是
int
,而不是
bool

public class FinalSiteModel 
{
    ...
    public List<Printer> Printers { get; set; }
    public int SelectedPrinter { get; set; }
}
公共类最终项目模型
{
...
公共列表打印机{get;set;}
公共int-SelectedPrinter{get;set;}
}
然后在视图中

foreach(var printer in Model.Printers)
{
    <label>
        @Html.RadioButtonFor(m => m.SelectedPrinter, printer.Id, new { id = "" })
        <span>@printer.Name</span>
    </label>
}
foreach(Model.Printers中的var打印机)
{
@RadioButton(m=>m.SelectedPrinter,printer.Id,new{Id=”“})
@打印机名称
}
注意
radiobutton()
的第二个参数根据
Printer
Id
属性添加
value
属性,而
new{Id=”“}
删除
Id
属性,否则该属性将无效,因为该方法会生成重复项

SelectedPrinter
的值现在将包含提交表单时所选
Printer
Id


请注意,切勿尝试使用
new{name=“groupRadio”}
更改
name
属性-它不会绑定到您的模型。

您的单选按钮需要一个值来绑定到您的
选定的
属性。但是,该属性必须是
int
,而不是
bool

public class FinalSiteModel 
{
    ...
    public List<Printer> Printers { get; set; }
    public int SelectedPrinter { get; set; }
}
公共类最终项目模型
{
...
公共列表打印机{get;set;}
公共int-SelectedPrinter{get;set;}
}
然后在视图中

foreach(var printer in Model.Printers)
{
    <label>
        @Html.RadioButtonFor(m => m.SelectedPrinter, printer.Id, new { id = "" })
        <span>@printer.Name</span>
    </label>
}
foreach(Model.Printers中的var打印机)
{
@RadioButton(m=>m.SelectedPrinter,printer.Id,new{Id=”“})
@打印机名称
}
注意
radiobutton()
的第二个参数根据
Printer
Id
属性添加
value
属性,而
new{Id=”“}
删除
Id
属性,否则该属性将无效,因为该方法会生成重复项

SelectedPrinter
的值现在将包含提交表单时所选
Printer
Id


请注意,切勿尝试使用
new{name=“groupRadio”}
更改
name
属性-它不会绑定到您的模型。

幸好您一直在Stephen身边,再次感谢:)幸好您一直在Stephen身边,再次感谢:)