[MVC] 自定义ActionSelector,根据参数选择Action
2012-12-25 11:35
393 查看
很多时候我们会根据UI传入的参数,呈现不同的View。也就是对于同一个Action如何根据请求数据返回不同的View。通常情况下我们会按照如下方法来写,例如:
这个时候Action 里面会有很多的if else. 其实,我们忽略了MVC 提供的 ActionNameSelectorAttribute. 我们完全可以自定义ActionNameSelectorAttribute来实现上诉功能。 MVC中的ActionNameAttribute就是继承自ActionNameSelectorAttribute。达到多个Action对应同一个ActionName的目的。
自定义TreeNodeActionSelectorAttribute
改造Action
有些时候我们可能需要根据用户属于不同的角色,返回不同的View。这种情况也可以通过自定义ActionNameSelectorAttribute来实现。具体如何做请参考:http://www.cnblogs.com/anytao/archive/2009/04/22/1440883.html
[AcceptVerbs(HttpVerbs.Post)] public ActionResult LoadTreeNodes(TreeViewItem treeViewItem) { var nodeText = treeViewItem.Text.ToLower(); if (nodeText == "videos") { …… return View("videos"); } if (nodeText == "news") { …… return View("news"); }return View("index"); }
这个时候Action 里面会有很多的if else. 其实,我们忽略了MVC 提供的 ActionNameSelectorAttribute. 我们完全可以自定义ActionNameSelectorAttribute来实现上诉功能。 MVC中的ActionNameAttribute就是继承自ActionNameSelectorAttribute。达到多个Action对应同一个ActionName的目的。
自定义TreeNodeActionSelectorAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class TreeNodeActionSelectorAttribute : ActionNameSelectorAttribute { public string TreeNodeText { get; private set; } public TreeNodeActionSelectorAttribute(string nodeText) { if (String.IsNullOrEmpty(nodeText)) { throw new ArgumentException("nodeText"); } this.TreeNodeText = nodeText; } public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) { var text = controllerContext.RequestContext.HttpContext.Request["text"]; return String.Equals(this.TreeNodeText, text, StringComparison.OrdinalIgnoreCase); } }
改造Action
[ActionName(“LoadTreeNodes”)] [TreeNodeActionSelector (“videos”)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult LoadVideoNodes(TreeViewItem treeViewItem) { …… return View("videos"); } [ActionName(“LoadTreeNodes”)] [TreeNodeActionSelector (“news”)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult LoadNews(TreeViewItem treeViewItem) { …… return View("news"); } ......
有些时候我们可能需要根据用户属于不同的角色,返回不同的View。这种情况也可以通过自定义ActionNameSelectorAttribute来实现。具体如何做请参考:http://www.cnblogs.com/anytao/archive/2009/04/22/1440883.html
相关文章推荐
- 艾伟:[一步一步MVC]第一回:使用ActionSelector控制Action的选择
- [一步一步MVC]第一回:使用ActionSelector控制Action的选择
- Asp.net Mvc之Action如何传多个参数
- MVC学习系列2--向Action方法传递参数
- MVC扩展Url.Action方法解决复杂对象参数问题
- mvc控制器的action参数问题
- ASP.NET Web API是如何根据请求选择Action的?[下篇] 【转】
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- Custom Action Method Selector in MVC
- Asp.Net MVC 中获取Controller, Action 和参数的值
- MVC使用Exception过滤器自定义处理Action的的异常
- ASP.NET MVC 4 - ActionMethodSelectorAttribute
- MVC扩展(ActionNameSelectorAttribute vs ActionMethodSelectorAttribute)
- FineUIMvc随笔(4)自定义回发参数与自定义回发
- asp.net MVC 中 action中的参数有是集合的属性 视图中表单的设置方式
- Asp.net Mvc Codeplex Preview 5 第三篇 实现Action参数传递繁杂类型
- Asp.net Mvc之Action如何传多个参数
- MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
- MVC @Url.Action接受多个参数
- MVC View中获取action、controller、area名称、参数