问题描述
有没有办法来枚举集合传递给ActionMethod(在GET)自动?
例如,如果我有以下枚举定义:
枚举DaysEnum {周六,周日,周一,周二,周三,周四,周五};
和我有一个ActionMethod定义:
的ActionResult SampleActionMethod(列表< DaysEnum>天)
有没有一种方法,我可以呈现,在视图中,将通过DayEnums集合中的URL。是这样的:
VAR日子她念叨=新的List< DaysEnum> {DaysEnum.Sat,DaysEnum.Sun,DaysEnum.Mon};Url.Route(新{天=日子她念叨着,控制器=无所谓,行动=SampleActionMethod});
默认模型联似乎并不支持这一点,因为我目前得到以下渲染:
HTTP://.../System.Collections.Generic.List`1 [DaysEnum]
我知道我可以通过手动压扁集合,也就是说,一个破折号deliniated串做到这一点,然后重新在ActionMethod收集,但我一直在寻找的东西更优雅。各种 帖子谈论传入的集合,但更多的是在做岗位时。
<%= 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!