我有一个主导航,其中一个列表项呈现一个动作:
[ChildActionOnly]
public ActionResult BuildMenu(String category = null) {
ViewBag.SelectedCategory = category;
return View("~/Views/Article/CategoriesList.cshtml", this.GetItems());
}
其中GetItems
方法是:
[NonAction]
public IEnumerable<String> GetItems() {
return this.session.Query<Article>()
.Select(x => x.Category)
.Distinct().ToList()
.OrderBy(x => x);
}
发布文章时,可以指定如下类别:Fringe Division
. 所以在地址栏中它看起来像Fringe%20Division
.
在视图中(菜单部分)我有这个:
@model IEnumerable<String>
@{ Layout = null; }
<ul class="transparent-custom">
@foreach(var link in Model) {
<li>@Html.RouteLink(
link,
new {
controller = "Article", action = "Index",
category = link
},
new {
@class = link == ViewBag.SelectedCategory ? "selected" : ""
}
)
</li>
}
</ul>
如果我在这里应用类似的东西category = Url.ToUrlFriendly(link)
(用破折号或任何其他字符替换所有不可接受的字符),而它在地址栏中看起来很酷,我的控制器无法识别该类别(这很明显:它与原来的不同):
public ActionResult Index(String category, Int32? page) {
// there's no such a category in DB...
ViewBag.CurrentCategory = category;
if(category == "All") {
return View(this.GetAllArticles().ToPagedList(page ?? 1, this.PageSize));
}
var entries = this.session.Query<Article>()
.Where(c => c.Category == category)
.OrderBy(d => d.CreatedOn);
return View(entries.ToPagedList(page ?? 1, this.PageSize));
}
我如何以最好的方式处理它?谢谢!