MVC 2 - 将枚举传递给CheckBoxFor

ilk*_*kin 7 c# asp.net asp.net-mvc enums asp.net-mvc-2

我们假设我们有一个模型:

public class Document
{
    public string Name { get; set;}
    public List<DayOfWeek> WeekDays { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

是否可以呈现代表该模型的星期几的复选框?我搜索了互联网,但没有找到任何解决方案.

我的意思是它有效,CheckBoxFor(model=> model.SomeProperty)但是如果SomeProperty是的话它就不起作用List<DayOfWeek>.DayOfWeek这是一个枚举.

提前致谢.

Dan*_*son 18

这需要更改你的DayOfWeek枚举,但我赞成将它作为一个标志(更少杂乱,只有一个值等).有趣的是,微软也在他们的enum flags文档中使用了一周中的几天.

使用位标志的DayOfWeek枚举:

[Flags]//<-- Note the Flags attribute
public enum DayOfWeek
{
  Monday = 1,
  Tuesday = 2,
  Wednesday = 4,
  Thursday = 8,
  Friday = 16,
  Saturday = 32,
  Sunday = 64,
}
Run Code Online (Sandbox Code Playgroud)

模型:

public class Document
{
  public string Name { get; set;}

  //Note that WeekDays is no longer a collection.
  public DayOfWeek WeekDays { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

视图:

<% foreach(DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek))) { %>
  <label>
    <!-- The HasFlag stuff is optional and is just there to show how it would be populated if you're doing a `GET` request. -->
    <input type="checkbox" name="WeekDays[]" value="<%= (int)dayOfWeek%>" <%= Model.WeekDays.HasFlag(dayOfWeek) ? "checked='checked'" : "" %>" />
    <%= dayOfWeek %>
  </label>
<% } %>
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public ActionResult MyPostedPage(MyModel model)
{
  //I moved the logic for setting this into a helper because this could be re-used elsewhere.
  model.WeekDays = Enum<DayOfWeek>.ParseToEnumFlag(Request.Form, "WeekDays[]");
  ...
}
Run Code Online (Sandbox Code Playgroud)

ParseToEnumFlag的快速帮助:

public static class Enum<T>
{
  public static T ParseToEnumFlag(NameValueCollection source, string formKey)
  {
    //MVC 'helpfully' parses the checkbox into a comma-delimited list. We pull that out and sum the values after parsing it back into the enum.
    return (T)Enum.ToObject(typeof(T), source.Get(formKey).ToIEnumerable<int>(',').Sum());
  }
}
Run Code Online (Sandbox Code Playgroud)

背景: 枚举标志值在几何系列(1,2,4,8 ...)中的原因是,当这些值加在一起时,只有一种可能的组合.例如,我们知道31只能是周一,周二,周三,周四和周五(1 + 2 + 4 + 8 + 16).

更新 - 2012年9月3日

看来我错过了ToIEnumerable()这是我们源代码中的扩展.它需要一个分隔的字符串并将其转换为IEnumerable,因此非常适合逗号分隔的数字.感谢@escist的单挑局.

public static IEnumerable<T> ToIEnumerable<T>(this string source, char delimiter)
{
  return source.Split(new char[] { delimiter }, StringSplitOptions.RemoveEmptyEntries).Select(x => (T)Convert.ChangeType(x, typeof(T)));
}
Run Code Online (Sandbox Code Playgroud)

  • @escist,请参阅我关于ToIEnumerable的更新. (2认同)

bma*_*ini 12

您可以枚举枚举的值并手动创建复选框.name对每个复选框使用相同的内容将在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 %>" />
<% } %>
Run Code Online (Sandbox Code Playgroud)

你的行动方法是这样的:

public ActionResult Save(DayOfWeek[] dayofweek)
{
     // Do Stuff
}
Run Code Online (Sandbox Code Playgroud)