views:

95

answers:

3

I'm following a tutorial on creating the NerdDinner using ASP.Net MVC. However, I'm using Visual Studio 2010 Ultimate edition and there was only MVC2 to choose from.

So I've following the tutorial so far, and everything is really clicking and being really well explained, until this little hitch.

The guide is asking me to create new methods on a Controller file like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NerdDinner.Controllers
{
    public class DinnersController : Controller
    {
        public void Index(){
            Response.Write("<h1>Coming Soon: Dinners</h1>");
        }

        public void Details(int id) {
            Response.Write("<h1>Details DinnerID: " + id + "</h1>");
        }
    }
}

However, when I created the Controllers file, Visual Studio created an Index method already, but it looks vastly different to what the tutorial shows. Maybe this is the new way to do things using MVC2?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NerdDinner.Controllers
{
    public class DinnersController : Controller
    {
        //
        // GET: /Dinners/

        public ActionResult Index()
        {
            return View();        
        }
    }
}

My question is, how can I reproduce the Details and Index method (they're in MVC) to the MVC2 way?

Is this even relevant? Thank you!

+1  A: 

You can do that following 2 different ways:

  1. By passing id through ViewData

    public ActionResult Index()
    {
        ViewData["id"] = 10;
        return View();
    }
    
    
    <%= Html.Encode(ViewData["id"]) %>
    
  2. By passing id through view object

    public ActionResult Index2()
    {
        var id = 11;
        return View(id);
    }
    
    
    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<int>" %>
    ...
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <h2><%= Model %></h2>
    </asp:Content>
    
zerkms
I guess my question really is why does MVC2 using ActionResult as the return object, when in MVC the return type is VOID. Any ideas?
Sergio Tapia
Because in MVC the method returns nothing - it just send the data directly into response. In MVC2 method returns data (`ActionResult`) which will be later used by front controller to send it back to client
zerkms
+2  A: 

The latter way is correct, all HTML output should go through a View unless you are doing something exceptional. I am surprised that the book is telling you to use voids for Actions. This is not new to MVC2, I think maybe there is something wrong in the book!

Actions have a return type of ActionResult, which is really just a generic base type that might be HTML, a redirect, or a file download.

View() is a method on Controller. It will automatically look for a View with the same name as your Action. So DinnersController.Index() will return the view located at Views/Dinners/Index.aspx.

In fact, if you right-click on the word View() it will give you the option to add a new view and put it in the right place. The view is where your HTML should be.

Matt Sherman
The book does this simply to demonstrate how routing works without having to setup a view. The tutorial later tells you to replace the void with the ActionResult return type when it explains the concept of views. Yeah, it's a bit weird.
Bayard Randel
Precisesly, I feel like a douche for tarnishing this tutorial a bit, but they really do teach you later on that what you were doing before (the reason this question exists) is plain wrong.
Sergio Tapia
A: 

The tutorial example shown is not showing the best practice in MVC. It does do well in demonstrating a quick "Hello World" example.

The best practice should be to return the appropriate ActionResult because doing this would make it easier for you to unit test. Using the Response object directly should be avoided if possible.

Hope this helps.

Syd