比方说,我有一个ColorsController
和一个Colors/Index.cshtml
查看该控制器。 我想使用户能够输入形式在asp.net mvc中把url解析逻辑放在哪里?
Colors/Index/<coma separated list of colors>
的URL,然后让字符串“abc”打印为用户传递,在相同颜色的每种颜色。 一个URL的一个例子是
Colors/Index/red,white,green,yellow
现在,我已经定义了路由方式如下:
routes.MapRoute(
"Default",
"{controller}/{action}/{color}",
new { controller = "Colors", action = "Index", color = "black" }
,这里是控制器的代码:
public ActionResult Index(string colorsParams) {
string[] colors = colorsParams.Split(',');
return View(); //currently does not use colors
}
我的问题现在正在如何从这里开始。我应该直接将colorsParams传递给视图并让它解析信息吗?我应该在模型中做这种逻辑吗?在这里Index()方法?我想说,这种“解析”逻辑似乎不适合模型。另一方面,我会冒险说这个观点也不是正确的地方。这个怎么做?
感谢
你能不能解释一下如何在模型中做到这一点?我也相信通过ViewData/ViewBag将数据传递给视图看起来有点笨拙(尽管在这样一个小问题中这不是一个大问题)。 – 2011-02-14 00:57:03