tags:

views:

1776

answers:

1

how get ViewData in page load using Asp.net MVC and javascript

+2  A: 

You don't need a page load method in ASP.NET MVC. The view data and model is all accessible in the MVC ViewPage directly through the ViewData property because the Controller feeds the view page's ViewData.

Say you have a MyController in MyProject/Controllers/MyController with the following action:

public ActionResult Do() {
    ViewData["MyInt"] = 64;
    ViewData["MyString"] = "MyString";
    return View();
}

And in your Do view in MyProject/Views/My/Do.aspx you can access the viewdata directly:

<%= ViewData["MyInt"] %>
<%= ViewData["MyString"] %>

You can access them in the code-behind files as well because the viewpage inherits from System.Web.Mvc.ViewPage that has the ViewData property. You can read more about it here.

Spoike