开发者

Passing Data to View Master Pages base on login user

开发者 https://www.devze.com 2023-02-25 17:15 出处:网络
I\'ve a problem with my mvc application, mvc 3 using spark view eng开发者_如何学Goine. I want to bind a dropdownlist that attached to application.spark (master page) base on HttpContext.User.Identity.

I've a problem with my mvc application, mvc 3 using spark view eng开发者_如何学Goine. I want to bind a dropdownlist that attached to application.spark (master page) base on HttpContext.User.Identity. the problem is..where I should place the ViewData that contain the SelectList as my DropDownList datasource ? This dropdown list will be accessed in all page in my application. there is an article about it : http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs, but it didn't solved my problem, since I can't got User.Identity from ApplicationController.

any idea ?


  • You could create your own base Controller and override the OnActionExecuting method.

    protected override void OnActionExecuting(ActionExecutingContextfilterContext)
    {
        var userName = User.Identity.Name;
        ViewData["MySelectList"] = new SelectList(AllUsers, "Id", "UserName", userName );
    }
    
  • You could create an action filter and override the 'OnActionExecuting' method - then apply that filter to every controller.

     public class MyCustomActionFilterAttribute : ActionFilterAttribute
     {
       public override void OnActionExecuting(ActionExecutingContext filterContext)
       {
         var userName =  filterContext.RequestContext.HttpContext.User.Identity.Name;
          filterContext.Controller.ViewData["MySelectList"] = new SelectList(AllUsers, "Id", "UserName", userName);
       }
     }
    
    [MyCustomActionFilter]
    public class HomeController:Controller
    {....
    }
    
0

精彩评论

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