Where do the parameters come from for action that gets invoked by Html.RenderAction?
I tried to use the debugger but I can't seem to get anywhere. I can't step into Html.RenderAction(), it's in my master page.
I read that it gets the value "automatically" through routing. How does that work?
// "Nav" is the name of the controller housing the "Menu" action
// This is called in Site.Master
<% Html.RenderAction("Menu", "Nav"); %>
// where does "category" come from?
public ViewResult M开发者_如何学Goenu(string category)
{
}
I made this according to a book, but I can't find an explanation in there. The category supposedly comes from the URL automatically into the parameter.
On a related note: Do you advise to download the source code for MVC to work properly, or would that complicate my efforts more than it would help?
The Category parameter is being picked up from the following routing entry
routes.MapRoute(null, "{category}", // Matches ~/Football or ~/AnythingWithNoSlash
new { controller = "Products", action = "List", page = 1 }
);
so if /Football is entered then it is supplied as a parameter to the ViewResult Menu in the
which in turn calls
public ViewResult List(string category, int page = 1)
{
var productsToShow = (category == null)
? productsRepository.Products
: productsRepository.Products.Where(x => x.Category == category);
var viewModel = new ProductsListViewModel {
Products = productsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
PagingInfo = new PagingInfo {
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = productsToShow.Count()
},
CurrentCategory = category
};
return View(viewModel); // Passed to view as ViewData.Model (or simply Model)
}
so later within the view master when the render action is called
<% Html.RenderAction("Menu", "Nav"); %>
it can pick up on the category parameter in the route i.e. {category}
public ViewResult Menu(string category)
{
// Just so we don't have to write this code twice
Func<string, NavLink> makeLink = categoryName => new NavLink
{
Text = categoryName ?? "Home",
RouteValues = new RouteValueDictionary(new {
controller = "Products", action = "List",
category = categoryName, page = 1
}),
IsSelected = (categoryName == category)
};
// Put a Home link at the top
List<NavLink> navLinks = new List<NavLink>();
navLinks.Add(makeLink(null));
// Add a link for each distinct category
var categories = productsRepository.Products.Select(x => x.Category);
foreach (string categoryName in categories.Distinct().OrderBy(x => x))
navLinks.Add(makeLink(categoryName));
return View(navLinks);
}
}
The Html.RenderAction
call renders the action with method name "Menu"
, in the controller "Nav"
.
The routing file contains patterns for how to populate method parameters (and resolve overloaded actions). The routing file is usually found in ~/Global.asax
, in the RegisterRoutes
method. This file should contain multiple calls to RouteCollection#MapRoute
, which maps a certain URL pattern to a new object with particular variables.
Your routes must include a single string mapping somewhere, which captures anything without a slash into a variable called category
. That is then passed to the Menu
action method.
Addendum: Try looking here for more information.
精彩评论