我要过滤的两个链接:
@Html.ActionLink("Customer 1", "Index", new { customer = 1 })
@Html.ActionLink("Project A", "Index", new { project = "A" })
我的带过滤器的 Controller :
public ViewResult Index(int? customer, int? project) {
var query = ...
if (customer != null) {
query = query.Where(o => o.CustomerID == customer);
}
if (project != null) {
query = query.Where(o => o.ProjectID == project);
}
return View(query.ToList());
}
我现在可以过滤客户或项目,但不能同时过滤两者!
如果我单击客户 1,
url = Object?customer=1
如果我单击项目 A,
url = Object?project=a
我希望能够先点击客户 1,然后点击项目 A 并获得
url = Object?customer=1&project=a
这是可能的还是我应该用另一种方式来做?
谢谢!
最佳答案
正确的方法是将带有各种参数的模型返回到您的 View 中。
型号
public class TestModel {
public int? Customer { get; set; }
public int? Project { get; set; }
public List<YourType> QueryResults { get; set; }
}
查看
@model Your.Namespace.TestModel
...
@Html.ActionLink("Project A", "Index", new { customer = Model.Customer, project = Model.Project })
Controller
public ViewResult Index(TestModel model) {
var query = ...
if (model.Customer != null) {
query = query.Where(o => o.CustomerID == model.Customer);
}
if (model.Project != null) {
query = query.Where(o => o.ProjectID == model.Project);
}
model.QueryResults = query.ToList();
return View(model);
}
关于asp.net-mvc - ASP.NET MVC ActionLink 保留 "old"路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350562/