我有一个主导航和一个列表项呈现一个动作:ASP.NET MVC路由。空格和类别
[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));
}
如何处理它的最佳方式? 谢谢!