Kje*_*sen 10 c# asp.net-mvc asp.net-mvc-5
说我有一个动作方法:
[HttpGet]
public ActionResult Search(List<int> category){
...
}
Run Code Online (Sandbox Code Playgroud)
MVC模型绑定的工作方式,它需要一个类似的列表:
/search?category=1&category=2
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
Url.Action("Search", new {category=???}) //Expect: /search?category=1&category=2
Run Code Online (Sandbox Code Playgroud)
var categories = new List<int>(){1,2}; //Expect: /search?category=1&category=2
Url.Action("Search", new {category=categories}) //does not work,
Run Code Online (Sandbox Code Playgroud)
Sam*_*m B 16
而不是使用匿名类型,构建一个RouteValueDictionary.将参数格式化为parameter[index].
@{
var categories = new List<int>() { 6, 7 };
var parameters = new RouteValueDictionary();
for (int i = 0; i < categories.Count; ++i)
{
parameters.Add("category[" + i + "]", categories[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,
@Url.Action("Test", parameters)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14794 次 |
| 最近记录: |