2

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?

2
  • does your "Add New" link needs to be a hyperlink? Can you make that a button? Commented Dec 2, 2011 at 16:04
  • @Corneliu I guess I could, but we were using hyperlinks for this purpose for all the other forms. Commented Dec 2, 2011 at 16:10

2 Answers 2

4

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;
});
Sign up to request clarification or add additional context in comments.

1 Comment

That's the sort of thing I had in mind! Thank you! And thanks for the code!
1

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" />
}

2 Comments

You can style the button to look like a hyperlink, if you need
Thank you! I was wondering if something like that might be possible, but I thought it might be complicated. (More complicated than doing it in jQuery? Maybe!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.