MVC 2 - 传递枚举CheckBoxFor [英] MVC 2 - Passing enum to CheckBoxFor

查看:216
本文介绍了MVC 2 - 传递枚举CheckBoxFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个模型:

public class Document
{
    public string Name { get; set;}
    public List<DayOfWeek> WeekDays { get; set; }
}

是否有可能呈现重新一周的present天内该模型复选框?
我在网上搜索,但没有发现任何解决方案。

Is it possible to render checkboxes that represent days of week for that model? I've searched the internet but did not find any solution.

我的意思是它的工作原理蒙山 CheckBoxFor(型号=&GT; model.SomeProperty),但它不工作,如果 SomeProperty 列表&LT;&DAYOFWEEK GT; 星期几这里是一个枚举。

I mean it works whith CheckBoxFor(model=> model.SomeProperty) but it does not work if SomeProperty is List<DayOfWeek>. DayOfWeek here is an enumeration.

先谢谢了。

推荐答案

您可以枚举枚举的价值和手动创建的复选​​框。使用相同的名称每个复选框将提交他们作为ActionMethod的数组。

You can enumerate on the values of the enum and manually create the checkboxes. Using the same name for each checkbox will submit them as an array in the ActionMethod.

<% foreach(var value in Enum.GetValues(typeof(DayOfWeek))) { %>
     <% var name = Enum.GetName(typeof(DayOfWeek), value); %>
     <label for="dayofweek<%=value %>"><%=name %></label>
     <input type="checkbox" id="dayofweek<%=value %>" name="dayofweek" value="<%=value %>" />
<% } %>

您的操作方法是这样的:

Your action method would be something like:

public ActionResult Save(DayOfWeek[] dayofweek)
{
     // Do Stuff
}

这篇关于MVC 2 - 传递枚举CheckBoxFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆