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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 87
    Comment on it

    ViewBag in 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>();
          ViewBag.Student = Student;
          return View();

    View Corresponding to Index action using ViewBag:

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


 0 Comment(s)

Sign In

Sign up using

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: