0

In my website with paging and search options i have to redirect the user and pass the search options. With this action I don't want to pass the search model with the URL (ex. http://localhost:1234/?Project.Models.TestModel=....).

My actionlink right now is as follows:

@Html.ActionLink(i.ToString(), "Index", new { idtrips = Model.idTrips, inituser = Model.initUser, date = Model.date.ToString("ddMMyyyy"), page = i})

When clicked this results in a following header: http://localhost:58763/?idtrips=0&inituser=0&date=05042016&page=5

My question is: Can you somehow add if clauses to the Html.ActionLink to only give values if they are needed. For example I only have a date set, then I want the Url to be: http://localhost:58763/?date=05042016&page=5. The other values have a default value in my controller so this url will work but i'm not able to generate it.

My controller:

public ActionResult Index(long idtrips = 0, long inituser = 0, string date = "", int page = 1)
    { ... }

What i'm looking for is something like this,:

@Html.ActionLink(i.ToString(), "Index", new { if(Model.idTrips > 0) { idtrips = Model.idTrips,} page = i})

1 Answer 1

1

You could probably accomplish this with a ternary statement (e.g. an inline-if statement) :

new { idtrips = Model.idTrips > 0 ? Model.IdTrips : 0, page = i}

Since you have a default parameter value for idtrips, this should only set it to something else if it is greater than 0.

If you don't want the value to appear at all, you could consider simply making it a nullable long long? parameter (and ensure this matches on your Model as well):

public ActionResult Index(long? idtrips = 0, ... ) { ... }

Using this approach, you could simply set it as expected :

new { idtrips = Model.idTrips, page = i}

And if Model.idTrips was null (e.g. the default value), then it would point to :

Controller/Index?page=1

However, if it was any non-null value, it would render the querystring parameter as expected :

Controller/Index?idtrips=42&page=1  
Sign up to request clarification or add additional context in comments.

3 Comments

But this still will give me the URL "localhost:1234/?Idtrips=0&page=5" And that is what I want to avoid. it should be like new { page = 5 } or new { Idtrips = 1, page = 5 } and inline. not with an if or switch statement over the Html.ActionLink
You may want to consider making your idtrips parameter nullable (and nullable on your Model). This would give you the intended effect that you are looking for (i.e. no display of your idtrips parameter when null, although it would default to 0, and it would otherwise display the parameter).
I changed them to nullable values and it works how I want. Thank you for your fast help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.