[英]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.