2

My two links to filter:

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

My Controller with filtering:

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

I can now filter on either customer or project but not on both at the same time!

If I click Customer 1, url = Object?customer=1

If I click Project A, url = Object?project=a

I would like to be able to first click Customer 1 and then Project A and get url = Object?customer=1&project=a

Is this possible or should I do it in another way?

Thanks!

Cotten
  • 8,787
  • 17
  • 61
  • 98

2 Answers2

2

Why not use something like this for the second link:

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

This way the customer parameter is passed in when you have it provided, but it will pass NULL when empty.

Garrett Vlieger
  • 9,354
  • 4
  • 32
  • 44
1

The correct way to do this is to return a model with your various parameters to your view.

Model

public class TestModel {
   public int? Customer { get; set; }
   public int? Project { get; set; }
   public List<YourType> QueryResults { get; set; }
}

View

@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);
}
Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124