2

我的两个过滤链接:

@Html.ActionLink("Customer 1", "Index", new { customer = 1  })
@Html.ActionLink("Project A", "Index", new { project = "A"  })

我的带有过滤功能的控制器:

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

这是可能的还是我应该以其他方式做到这一点?

谢谢!

4

2 回答 2

2

为什么不对第二个链接使用这样的东西:

@Html.ActionLink("Project A", "Index", new { customer = ViewContext.RouteData["customer"],  project = "A"  })

这样,客户参数在您提供时传入,但在为空时将传递 NULL。

于 2012-07-05T18:53:24.097 回答
1

执行此操作的正确方法是将具有各种参数的模型返回到您的视图中。

模型

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 })

控制器

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);
}
于 2012-07-05T18:54:33.410 回答