通过枚举的集合ASP.NET MVC ActionMethod [英] Pass collection of enums to ASP.NET MVC ActionMethod

查看:96
本文介绍了通过枚举的集合ASP.NET MVC ActionMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来枚举集合传递给ActionMethod(在GET)自动?

例如,如果我有以下枚举定义:

 枚举DaysEnum {周六,周日,周一,周二,周三,周四,周五};

和我有一个ActionMethod定义:

 的ActionResult SampleActionMethod(列表< D​​aysEnum>天)

有没有一种方法,我可以呈现,在视图中,将通过DayEnums集合中的URL。是这样的:

  VAR日子她念叨=新的List< D​​aysEnum> {DaysEnum.Sat,DaysEnum.Sun,DaysEnum.Mon};Url.Route(新{天=日子她念叨着,控制器=无所谓,行动=SampleActionMethod});

默认模型联似乎并不支持这一点,因为我目前得到以下渲染:

  HTTP://.../System.Collections.Generic.List`1 [DaysEnum]

我知道我可以通过手动压扁集合,也就是说,一个破折号deliniated串做到这一点,然后重新在ActionMethod收集,但我一直在寻找的东西更优雅。各种博客 <一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>帖子谈论传入的集合,但更多的是在做岗位时。


解决方案

 &LT;%= Html.ActionLink(测试枚举,SampleActionMethod,新RouteValueDictionary {
    {天[0],DaysEnum.Sun},{天[1],DaysEnum.Mon}
})%GT;

Is there a way to pass a collection of Enums to an ActionMethod (on a GET) automatically?

For example, if I have the following enum definition:

enum DaysEnum {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

and, I have an ActionMethod definition of:

ActionResult SampleActionMethod ( List<DaysEnum> days)

Is there a way I could render, in a View, a URL that would pass in a collection of DayEnums. Something like:

var someDays = new List<DaysEnum> {DaysEnum.Sat, DaysEnum.Sun, DaysEnum.Mon};

Url.Route(new { days = someDays, controller="whatever", action="SampleActionMethod"});

The default model binder doesn't seem to support this, since I'm currently getting the following rendered:

http://.../System.Collections.Generic.List`1[DaysEnum]

I know I can do this by manually flattening the collection to, say, a dash-deliniated string, and then recreate the collection in the ActionMethod, but I was looking at something more elegant. Various blog posts talk about passing in collections, but that is more about when doing POSTS.

解决方案

<%= Html.ActionLink("test enums", "SampleActionMethod", new RouteValueDictionary { 
    { "days[0]", DaysEnum.Sun }, { "days[1]", DaysEnum.Mon } 
}) %>

这篇关于通过枚举的集合ASP.NET MVC ActionMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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