开发者

asp.net mvc - value provider and collections

I'd like to开发者_运维知识库 access in my ActionFilter a property. This propert is a collection.

Usually I access the values using the valueprovider like this

filterContext.Controller.ValueProvider.GetValue("Prop");

but this isn't working in case of a collection.

Is there way to get my collection ?


You could use filterContext.ActionParameters. Example:

Model:

public class MyViewModel
{
    public IEnumerable<string> Collection { get; set; }
}

Action filter:

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var value = filterContext.ActionParameters["model"];
        // TODO: do something with the value
        base.OnActionExecuting(filterContext);
    }
}

Controller:

public class HomeController : Controller
{
    [MyActionFilter]
    public ActionResult Index(MyViewModel model)
    {
        return View();
    }
}

Request: /?collection[0]=foo&collection[1]=bar

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜