ASP.NET MVC – Controller to View

ka_vol1_100 A couple of weeks ago we looked at ASP.NET MVC routing in the MVC framework.  The routing controls which method in which controller gets called.

The obvious next question is, how do we get from the controller to the view?

First, we need to look at the general layout of our Views.

If you open up the sample project that we created, you’ll see that there are a few directories that have been created.  The one we want to take a look at today is the View directory.

You’ll see that under each View directory is a directory that has the same name as each of the controllers in the Controller directory as well as a directory named ‘Shared’ that has nothing to do with MVC directly.  Don’t worry about figuring that one out right now.

Under each of the directories that map to the controller, you’ll see that there is an ASPX file that maps to each of the methods in the controllers, or is otherwise called from those controllers.

The easiest way to get from the Controller action to the View it corresponds to is to return View() from that action, as in:

public ActionResult About()
    return View();

This would then call ~/Home/About

But what if the new page needs to have data sent along to it?

In this case, you can assign the data to the ViewData property.  The ViewData property works a lot like a Session object in that it is keyed.

So, to pass data you would use

ViewData["Key"] = objectData;

You can see that the sample project does this in the HomeController.Index method.

public ActionResult Index()
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    return View();

And you can see that the Index.aspx file picks it up later:

    <h2><%= Html.Encode(ViewData["Message"]) %></h2>
        To learn more about ASP.NET MVC visit 
        <a href="" 
        title="ASP.NET MVC Website"></a>.

Returning View() is not the only way of specifying the View we want to display from the Controller.  You can also return Redirect(), RedirectAction(), RedirectToRoute().

Related Post

  • ASP.NET Model View ControllerASP.NET Model View Controller Last week the ASP.NET Model View Controller framework was released as Release Candidate 1.  That's my cue to take a look at what we finally have available to us and to start a series explaining ho...
  • ASP.NET MVC – Model != BLL or DALASP.NET MVC – Model != BLL or DAL Last week I introduced the ASP.NET MVC framework by talking a bit about what the model, view and controller are. In the comments, John Meyer said, I respectfully disagree with your claim that t...
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it wrong, which...
  • ASP.NET MVC – RoutingASP.NET MVC – Routing One of the core features of ASP.NET MVC that makes everything "just work" is the concept of routing.  By specifying ahead of time what a route looks like, we can create links that look like r...
  • ASP.NET MVC in ActionASP.NET MVC in Action The Model-View-Controller pattern—universally known as MVC—provides a stable, testable approach to web application development by separating the major functions—or concerns—of an application int...