Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 用户只能选中一个复选框(html帮助程序MVC)_C#_Asp.net Mvc_Checkbox_Html Helper - Fatal编程技术网

C# 用户只能选中一个复选框(html帮助程序MVC)

C# 用户只能选中一个复选框(html帮助程序MVC),c#,asp.net-mvc,checkbox,html-helper,C#,Asp.net Mvc,Checkbox,Html Helper,以下代码为产品的每个折扣生成复选框。如果有三个折扣可用,将生成三个复选框。如何确保用户只能选择其中一个复选框?我不想使用单选按钮,因为我希望用户可以选择不单击其中任何一个。我希望用户只选择一个,以便在控制器中更容易处理数据。提前谢谢 <br /> <div> <strong>Discounts:</strong> @foreach (var discount in Model.Product.Discounts)

以下代码为产品的每个折扣生成复选框。如果有三个折扣可用,将生成三个复选框。如何确保用户只能选择其中一个复选框?我不想使用单选按钮,因为我希望用户可以选择不单击其中任何一个。我希望用户只选择一个,以便在控制器中更容易处理数据。提前谢谢

<br />
<div>
    <strong>Discounts:</strong>
        @foreach (var discount in Model.Product.Discounts)
        {
            <br/>
            @Html.CheckBox("Product.Discount", discount, true, new { Id = string.Format("{0}-{1}", Model.Product.CourseId, discount.DiscountId) })
            @discount.Description
            @discount.Percentage
        }
    <br/>
</div>

在html中,要链接单选按钮/复选框,必须为它们指定相同的名称

因此,添加一个name=pdiscount,您可以随意调用它

@Html.CheckBox("Product.Discount", discount, true, 
    new { Id = string.Format("{0}-{1}", 
           Model.Product.CourseId, discount.DiscountId), 
           name = "pdiscount" })

为“无”设置单选按钮怎么样?这将是明确的,并且也保留在GUI标准中。+1使用单选按钮-复选框专门用于多个选择。为什么要违抗传统?哦,我都没想到!你帮我省了很多工作。谢谢你!!