Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use route parameters in controller actions?
    primarykey
    data
    text
    <p>I am very new to routing in ASP.NET MVC so bare with me. </p> <p>I have the following controller:</p> <pre><code> public ActionResult Index(int? categoryId) { IList&lt;Item&gt; result = categoryId == null ? _itemsService.GetLast(20) : _itemsService.GetByCategory((int)categoryId); var viewModel = Mapper.Map&lt;IList&lt;Item&gt;, IList&lt;ItemsIndexViewModel&gt;&gt;(result); return View(viewModel); } </code></pre> <p>The nullable categoryId parameter refers to the id of a sub-category. In case nothing was passed, the last 20 items must be displayed, but if a sub-category id was passed, the items from that category should be displayed.</p> <p>But what I'm trying to go for is: <code>www.myDomain.com/Category/SubCategory</code> (ex: <code>/Electronics/Cell-Phones</code>)</p> <p>My attempt at writing a route is this:</p> <pre><code> routes.MapRoute( "ItemIndex", "{category}/{subcategory}", new {controller = "Item", action = "Index", categoryId = UrlParameter.Optional} ); </code></pre> <p>But I have no idea how to pass the values of the category and subcategory.</p> <p>Any help would be appreciated :)</p> <p><strong>UPDATE:</strong> Here are the route definitions I've got so far:</p> <pre><code> public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" }); routes.MapRoute( "ItemDetail", "Item/Detail/{itemId}", new { controller = "Item", action = "Detail" } ); routes.MapRoute( "ItemIndex", "Items/{category}/{subcategory}", new { controller = "Item", action = "Index", subcategory = UrlParameter.Optional } ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } </code></pre> <p>This is my controller I wish to map the route to:</p> <pre><code> public ActionResult Index(string category, string subcategory) { // IList&lt;Item&gt; result = string.IsNullOrEmpty(subcategory) ? _itemsService.GetLast(20) : _itemsService.GetByCategory(subcategory); IList&lt;Item&gt; result; if (string.IsNullOrEmpty(subcategory)) { if (string.IsNullOrEmpty(category)) { result = _itemsService.GetLast(20); } else { result = _categoryService.GetItemsInTopLevel(category); } } else { result = _itemsService.GetByCategory(subcategory); } var viewModel = Mapper.Map&lt;IList&lt;Item&gt;, IList&lt;ItemsIndexViewModel&gt;&gt;(result); return View(viewModel); } </code></pre> <p>And here's how I'm calling it from the View:</p> <pre><code>@model IList&lt;Sharwe.MVC.Models.ParentCategory&gt; &lt;div id="sharwe-categories"&gt; &lt;ul class="menu menu-vertical menu-accordion"&gt; @foreach(var topLevel in Model) { &lt;li class="topLevel"&gt; &lt;h3&gt; @Html.ActionLink(topLevel.Name, "Index", "Item", new { category = topLevel.Name }, new {@class = "main"} ) &lt;a href="#" class="drop-down"&gt;&lt;/a&gt; &lt;/h3&gt; &lt;ul&gt; @foreach (var childCategory in topLevel.Children) { &lt;li&gt;@Html.ActionLink(childCategory.Name, "Index", "Item", new RouteValueDictionary{ { "category" , topLevel.Name }, { "subcategory" , childCategory.Name }})&lt;/li&gt; } &lt;/ul&gt; &lt;/li&gt; } &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>When I click on a top level category, it works perfectly fine. But clicking on the sub-categories does not work. It actually redirects to <code>http://localhost/?Length=4</code>. I have no idea where this is coming from... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload