Exercise 3: Passing parameters to a Controller

Until now, you have been returning constant strings from the Controllers. In this exercise you will learn how to pass parameters to a Controller using the URL and querystring and then making the method actions respond with text to the browser.

Task 1 – Adding Genre Parameter to StoreController

In this task, you will use the querystring to send parameters to the Browse action method in the StoreController.

  1. If not already open, start Microsoft Visual Web Developer 2010 Express from Start | All Programs | Microsoft Visual Studio 2010 Express | Microsoft Visual Web Developer 2010 Express.
  2. In the File menu, choose Open Project. In the Open Project dialog, browse to Source\Ex03-PassingParametersToAController\Begin, select MvcMusicStore.sln and click Open. Alternatively, you may continue with the solution that you obtained after completing the previous exercise.
  3. Open StoreController class. To do this, in the Solution Explorer, expand the Controllers folder and double-click StoreController.[cs|vb].
  4. Change the Browse method, adding a string parameter to request for a specific genre. ASP.NET MVC will automatically pass any querystring or form post parameters named genre to this action method when invoked. To do this, replace the Browse method with the following code:

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex3 StoreController BrowseMethod – CSharp)

    C#

    // // GET: /Store/Browse?genre=Disco public string Browse(string genre) { string message = HttpUtility.HtmlEncode("Store.Browse, Genre = " + genre); return message; }
    FakePre-9f36af0c06934ad681a2267883dd01f3-cc97fd6a2dd54dd1a90213fa59d61404
    

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex3 StoreController BrowseMethod – VB)

    Visual Basic

    ' 'GET: /Store/Browse?genre=Disco Public Function Browse(ByVal genre As String) As String Dim message As String = HttpUtility.HtmlEncode("Store.Browse, Genre = " & genre) Return message End Function
    FakePre-16004a8aa15947d0a178160193794b73-b49b227396bf4e0c9adb31d65ca17d82
    

    Note:
    You are using the HttpUtility.HtmlEncode utility method to prevents users from injecting Javascript into the View with a link like /Store/Browse?Genre=<script>window.location=’https://hackersite.com’</script>.

    For further explanation, please visit this msdn article.

Task 2 – Running the Application

In this task, you will try out the Application in a web browser and use the genre parameter.

  1. Press F5 to run the Application.
  2. The project starts in the Home page. Change the URL to /Store/Browse?Genre=Disco to verify that the action receives the genre parameter.

    Figure 21

    Browsing /Store/Browse?Genre=Disco

  3. Close the browser.

Task 3 – Adding an Id Parameter Embedded in the URL

In this task, you will use the URL to pass an Id parameter to the Details action method of the StoreController. ASP.NET MVC’s default routing convention is to treat the segment of a URL after the action method name as a parameter named Id. If your action method has a parameter named Id then ASP.NET MVC will automatically pass the URL segment to you as a parameter. In the URL Store/Detail/5, Id will be interpreted as 5.

  1. Change the Details method of the StoreController, adding an int parameter called id. To do this, replace the Details method with the following code:

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex3 StoreController DetailsMethod – CSharp)

    C#

    // // GET: /Store/Details/5 public string Details(int id) { string message = "Store.Details, ID = " + id; return message; }
    FakePre-618addd3ad4c46afad92693376797a8d-c256465982d84b769c7aadae90b18bd9
    

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex3 StoreController DetailsMethod – VB)

    Visual Basic

    ' 'GET: /Store/Details/5 Public Function Details(ByVal id As Integer) As String Dim message As String = "Store.Details, ID = " & id Return message End Function
    FakePre-c92d79d7e5614a52bd1b89d186627f4c-963cade56f8d49dfad07fc4623b54cbd
    

Task 4 – Running the Application

In this task, you will try out the Application in a web browser and use the Id parameter.

  1. Press F5 to run the Application.
  2. The project starts in the Home page. Change the URL to /Store/Details/5 to verify that the action receives the id parameter.

    Figure 22

    Browsing /Store/Details/5

Next Step

Exercise 4: Creating a View