2012-07-05 2 views
2

내 두 개의 링크 필터하기 : 필터링ASP.NET MVC ActionLink 유지 "오래된"경로 인수

@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

을 클릭하면

, url = Object?project=a은 내가 먼저 고객 1을 클릭 할 수 다음 프로젝트 A와 url = Object?customer=1&project=a

을 좀하고 싶습니다

이인가

가능하거나 다른 방식으로해야합니까?

감사합니다.

답변

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); 
} 
+0

일이 올바른 방법입니다 –

2

왜 두 번째 링크에 대한이 같은 것을 사용하지 : 때 빈

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

고객 매개 변수는이 제공이있을 때 전달되는이 방법하지만 NULL을 전달합니다.