On Microsoft site: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3 I have found usage of HTML.ActionLink
. Author describes its parameters. In the 3rd point he refers to route parameter values. It looks like creating new object. I do not get it at all. The first parameter describes the Name
already. Why do wee need 3rd parameter?
The question: What is route parameter value in HTML.ActionLink and what is right syntax of ti?
<ul>
@foreach (var genre in Model)
{
<li>@Html.ActionLink(genre.Name,
"Browse", new { genre = genre.Name })</li>
}
</ul>
Controller:
public ActionResult Browse(string genre) {
//string message = HttpUtility.HtmlEncode("Store.Browse, Genre =" + genre);
var genreModel = new Genre { Name = genre };
return View(genreModel);
}
class Genre:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcMusicStore.Models {
public class Genre {
public string Name { get; set; }
}
}
Route parameter is passed to the query string of the Action request. In Controller action you see that the method signature is Browse(string genre), which means that parameter named genre is required. The third parameter of action link does just that - it adds a parameter genre, whose value is assigned to property Name of object genre.
The ActionLink parameters are mapped like this:
@Html.ActionLink(
genre.Name, // display of the link in a tag
"Browse", // name of the action, it redirects to public ActionResult Browse(string genre)
new {
genre = //name of the parameter for the controller action Browse(string **genre**)
genre.Name // value of the parameter
})