Ann L.
Ann L.

Reputation: 13975

dynamically generate query string for hyperlink based on field values

I have an ASP.Net MVC search page. It's a fairly standard list, but has a couple of dropdowns that filter the results. Those dropdowns and the search text input tag are wrapped in a form tag.

I also have a "Add New [entity]" hyperlink. The hyperlink goes to a Create view.

The thing is, the dropdowns from the search form also dictate values for some of the fields on the Create view. So I need to pass their values to the Create form.

The most straightforward way to do this (that I know of) would be to pass the relevant values in the Add New hyperlink's query string.

I can achieve this by changing the HREF value of my link every time a dropdown changes. But that involves several event handlers, and some of the dropdowns already have other event handlers tied to them. (There's a dependency between two of the dropdowns, just to make things more complicated.)

What I'd like to do is change the query string — gather the current values of the dropdowns and change it — between the click on the hyperlink and the actual execution of the hyperlink. Or emulate that happening.

Is this possible? And is it a good idea, or should I just stick with multiple event handlers changing the HREF?

Upvotes: 2

Views: 1753

Answers (2)

Corneliu
Corneliu

Reputation: 2942

If you can make "Add New" a button you could use a form with two independent submit buttons, each invoking a different action.

The technique is described in this blog http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/

Something like:

@using (Html.BeginForm("Action", "Post")) {

  <input type="submit" name="Search" value="Search" />
  <input type="submit" name="AddNew" value="Add New" />
}

Upvotes: 1

Evan
Evan

Reputation: 6115

why not add an event handler to your link and construct the link and then execute it?

$("a").click(function(event) {
  event.preventDefault();

  var url = "www.whatever.com/";

  var addedVars = "?firstVar=" + $('#dropdown1').val() + "&secondVar=" + $('#dropdown2').val();

  window.location = url + addedVars;
});

Upvotes: 4

Related Questions