patterncsharpMinor
Passing an array of values in Url.Action
Viewed 0 times
arraypassingactionvaluesurl
Problem
I need to pass an array of values in
Currently I'm doing:
To give me something like:
Is there a nicer/cleaner approach?
Url.Action.Currently I'm doing:
var redirectUrl = Url.Action("search", new {
q = criteria.Q,
advanced = criteria.Advanced,
salaryfrom = criteria.SalaryFrom,
salaryto = criteria.SalaryTo,
});
if (criteria.JobTypes != null)
redirectUrl += criteria.JobTypes.Aggregate(string.Empty, (a, x) => a += "&jobTypes=" + x);To give me something like:
/search?q=developer&advanced=false&salaryfrom=20000&salaryto=80000&jobTypes=Full%20Time&jobTypes=ContractIs there a nicer/cleaner approach?
Solution
I assume you "own" the controller?
If so, I'd serialize and deserialize the list as a comma separated string on each side.
It'd also be nice if criteria.JobTypes is an empty list instead of a possible null, then you don't need the ?? below.
And do
URL would look a bit nicer too:
If so, I'd serialize and deserialize the list as a comma separated string on each side.
It'd also be nice if criteria.JobTypes is an empty list instead of a possible null, then you don't need the ?? below.
var redirectUrl = Url.Action("search", new {
// ...
jobTypes = String.Join(criteria.JobTypes ?? new string[0], ",")
}And do
jobTypes.split(",") in the action.URL would look a bit nicer too:
/search?q=developer&advanced=false&salaryfrom=20000&salaryto=80000&jobTypes=Full%20Time,ContractCode Snippets
var redirectUrl = Url.Action("search", new {
// ...
jobTypes = String.Join(criteria.JobTypes ?? new string[0], ",")
}/search?q=developer&advanced=false&salaryfrom=20000&salaryto=80000&jobTypes=Full%20Time,ContractContext
StackExchange Code Review Q#11716, answer score: 5
Revisions (0)
No revisions yet.