How can I accomplish this type of URL in ASP.Net MVC2?

后端 未结 2 1478
长发绾君心
长发绾君心 2021-01-03 06:10

I have a table called Categories. I want the user to click from a list of Categories and then load a listing of all Auctions in that category.

Simple enough, right?<

2条回答
  •  长发绾君心
    2021-01-03 06:50

    Create one ActionResult:

    public class AuctionController : Controller
    {
        public ActionResult AuctionCategoryDetails(string categoryName)
        {
            var model = repository.GetAuctionsForCategory(categoryName);
            return View(model);
        }
    }
    

    Then create one route:

    routes.MapRoute(
        "AuctionCategoryDetails",
        "Auctions/{categoryName}",
        new { controller = "Auction", action = "AuctionCategoryDetails" });
    

    So when your displaying a list of categories (not individual details);

    <% foreach (var category in Model.Categories) { %>
       <%: Html.RouteLink("Category Details", "AuctionCategoryDetails", new { categoryName = category.CategoryName });
    <% } %>
    

    That will produce a list of links like this:

    Category Details
    Category Details
    

    Is that what your after?

提交回复
热议问题