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

后端 未结 2 1479
长发绾君心
长发绾君心 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:

    <a href="/Auctions/Clothes">Category Details</a>
    <a href="/Auctions/RealEstate">Category Details</a>
    

    Is that what your after?

    0 讨论(0)
  • 2021-01-03 06:58

    Yes, it's called URL routing. You map your categories to a single AuctionController action, which serves and displays category data dynamically based on what's in the URL.

    There's a tutorial on the ASP.NET MVC site that covers the ground basics.

    0 讨论(0)
提交回复
热议问题