我有一个名为Categories的表.我希望用户从类别列表中单击,然后加载该类别中所有拍卖的列表.
很简单吧?
我可以为每个类别创建一个动作,例如:
@H_404_6@public ActionResult Cellphones() public ActionResult Electronics public ActionResult Clothes public ActionResult Cars public ActionResult RealEstate这将生成以下URL:/ Auctions / Clothes和/ Auctions / RealEstate.正是我在寻找什么.
问题是这需要手动修补.当我添加一个类别时,我将不得不手动创建一个新动作,然后为它创建一个新视图.
有一个更好的方法吗?
解决方法
创建一个ActionResult:
@H_404_6@public class AuctionController : Controller
{
public ActionResult AuctionCategoryDetails(string categoryName)
{
var model = repository.GetAuctionsForCategory(categoryName);
return View(model);
}
}
然后创建一个路线:
@H_404_6@routes.MapRoute( "AuctionCategoryDetails","Auctions/{categoryName}",new { controller = "Auction",action = "AuctionCategoryDetails" });所以当你显示一个类别列表(而不是个别细节);
@H_404_6@<% foreach (var category in Model.Categories) { %> <%: Html.RouteLink("Category Details","AuctionCategoryDetails",new { categoryName = category.CategoryName }); <% } %>这将产生一个这样的链接列表:
@H_404_6@<a href="/Auctions/Clothes">Category Details</a> <a href="/Auctions/RealEstate">Category Details</a>那是你的追求吗?