NiklasJ
NiklasJ

Reputation: 580

Add query parameters to current url client side

I'm feeling a bit confused, it feels like this should be dead simple. (ASP.NET MVC3)

We have an Action, called "Product/List". We also have a textbox on the page, called "Expiry date". Now, if that is blank, we don't use it, if it's filled in, we select products with expiry date after that.

There's also a button to "update page", according to selected expiry date.

This needs to be added to the query string (or replaced), which seems to be simple;

$(button).bind('click', ...
  window.location.href = '@Url.Action(...)?ExpiryDate=' + ExpiryDate;
)

It works, but for me, this looks awful, and makes the adding of new parameters really annoying.

We can't use @Url.Action(..., new { ExpiryDate=... }), since this needs to be done client side after the textbox is used.

And, furthermore, we don't want to use a submit button and a HttpPost action; this is not a post, it is an update of the view (plus it breaks the browser history).

Most likely I'm missing something; what would be the preferred way to do this?

Upvotes: 0

Views: 366

Answers (1)

Peter
Peter

Reputation: 7804

Wrap your ExpiryDate input in a form (call it expiryDate) and do a form submit on it

You can choose GET or POST as your request VERB in your action method you can have a form collection to inspect for a null expiryDate.

Or use the ParameterBinding in MVC by have a parameter called expiryDate and check if its null. (you may have to make it nullable)

Upvotes: 1

Related Questions