Search code examples
asp.net-mvc-4viewbag

Can I send ViewBag from controller to anther Controller in MVC?


I have a method called GetPrivlidge() that I will use it in every controller ,this method is used for what will the current logged user will see and what will not see this is the method

 public void GetPrivlidge()
 {
        string name = System.Web.HttpContext.Current.User.Identity.Name;
        name = EncriptionAndDecription.Encrypt(name);
        int UserId = (from c in db.users where c.Arabicusername == name select c.Usersid).SingleOrDefault();
        SamahyatOfUser S = (from SS in db.SamahyatOfUsers where SS.User_Id == UserId select SS).SingleOrDefault();
        ViewBag.Pri = S;
        string RoleName = (from R in db.Roles where R.User_ID == UserId && R.UserName == name select R.RoleName).SingleOrDefault();
        if (RoleName.Equals(EncriptionAndDecription.Encrypt("Admin")))
        {
            ViewBag.ADMIN = true;
        }
        else
        {
            ViewBag.ADMIN = false;
        }
 }

I use ViewBag.Pri to check in layout for privilege ,I must send this ViewBag in every action so I decided to make controller that have all common methods this is my controller

 public class AllRequiredDataController : Controller
 {
    //
    // GET: /AllRequiredData/
    mts_exchangeEntities db = new mts_exchangeEntities();
    //this Method is used in privlidge that make user do thing and dont make anthor thing (Do or not Do)
    #region privlidge_Data
    public void GetPrivlidge()
    {
        string name = System.Web.HttpContext.Current.User.Identity.Name;
        name = EncriptionAndDecription.Encrypt(name);
        int UserId = (from c in db.users where c.Arabicusername == name select c.Usersid).SingleOrDefault();
        SamahyatOfUser S = (from SS in db.SamahyatOfUsers where SS.User_Id == UserId select SS).SingleOrDefault();
        ViewBag.Pri = S;
        string RoleName = (from R in db.Roles where R.User_ID == UserId && R.UserName == name select R.RoleName).SingleOrDefault();
        if (RoleName.Equals(EncriptionAndDecription.Encrypt("Admin")))
        {
            ViewBag.ADMIN = true;
        }
        else
        {
            ViewBag.ADMIN = false;
        }
    }
    #endregion
}

and in _layOut I take this ViewBag and make cast for in and work with privilege as this

FinalMts.Models.SamahyatOfUser S = (FinalMts.Models.SamahyatOfUser)ViewBag.Pri; 

and in action I make Controller object and call the method GetPrivlidge() as this

 AllRequiredDataController Required = new AllRequiredDataController();
    public ActionResult AddCurrencyPrices()
    {
        //get privlidg Data
        Required.GetPrivlidge();
        return View();
    }

but the layout don't see the ViewBag.Pri and error message say that

Object reference not set to an instance of an object.

is this controller can't see ViewBag if yes what should I do to deal with this ViewBag?


Solution

  • Each controller has its own ViewBag. Better is to inherit your controller form AllRequiredDataController and use this way

    class Controller1 : AllRequiredDataController
    {
        public ActionResult AddCurrencyPrices()
        {
            //get privlidg Data
            GetPrivlidge();
            return View();
        }
    }