Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of VIewBag and ViewData in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 364
    Comment on it

    Like we have state management in ASP.NET, ViewBag and ViewData are also used for providing the state management in MVC


    Use of ViewBag and ViewData:

    1 It helps to maintain data when you move from controller to view.
    2 It is used to pass data from controller to corresponding view.


    In View

    public ActionResult Index()
    {
        ViewBag.Name = "Himanshu Joshi";
        return View();
    }
    
    public ActionResult Index()
    {
        ViewData["Name"] = "Himanshu Joshi";
        return View();
    } 
    


    For retrieving values in forms

    @ViewBag.Name 
    @ViewData["Name"] 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: