简体   繁体   English

如何使用c#枚举在ASP.Net MVC中创建一组多选复选框?

[英]How do I make a group of multi-select checkboxes in ASP.Net MVC with c# enum?

I need to make a set of checkboxes where a user can select 1 or more markets. 我需要设置一组复选框,用户可以在其中选择一个或多个市场。 I would normally use a set of boolean values to accomplish this as it would be pretty strait forward and in my mind not take up much space in the database or take that long to load as they are only boolean values. 我通常会使用一组布尔值来完成此操作,因为这是相当困难的,在我看来,它们仅是布尔值,因此不会占用数据库中的太多空间或花费那么长的时间来加载它们。 It was suggested to me that I could use a single enum value in the database for the list of checkboxes. 我建议我可以在数据库中为复选框列表使用单个枚举值。 For example here is the enum 例如这里是枚举

[Flags]
public enum Markets 
{

    Canada = 2,
    USA = 4,
    Iceland = 8,
    Brazil = 16,
    Italy = 32,
    Japan= 64,

}

Then in the database I could store 14 for Canada Usa and Iceland. 然后在数据库中,我可以为加拿大,美国和冰岛存储14个。 I was wondering is enums the way to go in MVC for checkboxes and if so how would it work? 我想知道是枚举MVC中的复选框的方式吗?如果可以,它将如何工作?

Note i'm using C# MVC4 and entity framework 5 but i'm about to transition to MVC5 entity framework 6. 注意我正在使用C#MVC4和实体框架5,但是我将过渡到MVC5实体框架6。

This is pretty straight forward in ASP.NET MVC, just make sure you have a list of inputs on your page with the same name and that you are binding to a list on the backend. 在ASP.NET MVC中,这非常简单,只需确保页面上具有相同名称的输入列表并且绑定到后端的列表即可。

<form method="post" action="/Home/UpdateInts">
    <input type="checkbox" name="Markets" value="Canada" />
    <input type="checkbox" name="Markets" value="USA" />
    <input type="checkbox" name="Markets" value="Iceland" />
    <input type="checkbox" name="Markets" value="Brazil" />
    <input type="submit" />
</form>

That should do it. 那应该做。 You could use the HtmlHelpers to create them or just use Razor Syntax. 您可以使用HtmlHelpers来创建它们,也可以只使用Razor语法。

Here is some more helpful info. 这是一些更有用的信息。

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC 5 中的多选 CheckBoxList - Multi-select CheckBoxList in ASP.NET MVC 5 ASP.NET / C#-如何弹出带有列表框/多选控件的页面并将选定的ID发送回父页面 - ASP.NET / C# - How to pop up a page with a listbox / multi-select control and send the selected IDs back to the parent page ASP.net MVC(c#)中的复选框 - Checkboxes in ASP.net MVC (c#) 如何从ASP.NET MVC和C#中的数据库中为下拉列表选择值? - How do I select a value for a dropdownlist from a database in ASP.NET MVC and C#? 如何在C#中验证多个asp.net复选框 - How do I validate multiple asp.net checkboxes in C# 如何获取 Asp.Net Core DropdownList 以在 C# 枚举中显示 [Display()] 值? - How do I get an Asp.Net Core DropdownList to show the [Display()] values in a C# enum? ASP.NET中的多选下拉列表 - Multi-select dropdown list in ASP.NET 如何使用模型和控制器仅检查 ASP.NET MVC 中的两个复选框之一? - How do I make it possible to only check one of two checkboxes in ASP.NET MVC using models and controllers? 如何在 ASP.NET MVC 视图中对数据进行分组? - How do I group data in an ASP.NET MVC View? 我如何 select 并将数据行保存在表格中? C# ASP.NET MVC 5 - How can I select and save data row in a table? C# ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM