Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ViewBag in asp.net mvc

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 182
    Comment on it

    ViewBag in asp.net mvc

    • ViewBag is an object type which is a dynamic property of ControllerBase class.
    public Object ViewBag { get; }
    • ViewBag provides communication between controller and view. ViewBag passes data from controller to corresponding view.
    • ViewBag is similar to ViewData but is slower than ViewData.
    •  MVC 3.0 introduced ViewBag and is available in MVC 3.0 and above.ViewBag works only with .net framework 4.0 and above.
    • Typecasting is not required for complex data type.
    • The lifetime of ViewBag is short and is limited to current request only. It becomes null once the redirection occurs.

    Example of ViewBag:

    Action code within Controller:

    public ActionResult Index()
    {
          List<string> Student = new List<string>();
          Student.Add("Jignesh");
          Student.Add("suraj");
          Student.Add("mayank");
     
          ViewBag.Student = Student;
          return View();
    } 

    View Corresponding to Index action using ViewBag:

    <ul>
        <% foreach (var student in ViewBag.Student)
            { %>
        <li><%: student%></li>
        <% } %>
    </ul>

     

 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: