MVC 2 - 传递枚举CheckBoxFor [英] MVC 2 - Passing enum to 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屋!