views:

38

answers:

2

I am working on a web page that offers a form with a dropdown list to view data from a different year. The year is part of the URL routing that is configured. When the page loads, the URL can be something like: activitylog/department/year.

When the form is submitted on this page, the URL does not change, even though the user has selected a different year to view. Everything seems to actually be working correctly. When the year is changed from 2010 to 2008 on the form, the correct information for 2008 is displayed from the web server.

However, the URL displayed in the browser continues to display 2010. While this is not a real problem, I feel like it would be nice to display the direct URL for this particular dynamic page, ie: activitylog/IT/2008.

What is the preferred method to handle something like this? I believe I could use JQuery to capture the form submit, and generate the new URL using the year selected.

Is there a method built into MVC that could handle this that I am overlooking?

+1  A: 

Javascript is going to be your friend here.

You might want to create a javascript method to change the action property on the form, and either use the onSubmit event on the form, or have your button call this method directly (don't use type="button" and not type="submit" on your input) and have the method set the action and then submit the form.

NerdFury
+2  A: 

You could use jQuery to modify the form action before submitting:

$(function() {
    $('#idofyourform').submit(function() {
        var selectedYear = $('#idofyeardropdown').val();
        this.action = this.action.replace(/\/[\d]{4}$/, '/' + selectedYear);
        return true;
    });
});
Darin Dimitrov
I made a few mods for my particular situation, but this is working nicely. Thanks.
Swoop