I need to reload my page based on the selection of a value in a dropdownlist in MVC 3.
My dropdown is defined as such:
@Html.DropDownListFor(model => model.ID, new SelectList(Model.SchoolBranches, "ID", "Name", Model.ID), new { id = "Branches", name = "Branches"})
My script is defined as such so far:
<script type="text/javascript">
$(function () {
$("#Branches").change(function () {
var selected;
selected = $(this).val();
alert(selected);
// make a call to the Index action passing in the 'selected' value to reload the whole page
});
});
</script>
My selected ID is working fine as the alert show the correct ID on change. Just can’t find any examples that show how to navigate back to the index action and send the new ID. All the samples I have found show partial page or such refresh using ajax. I need the whole page reloaded.
Thanks
UPDATE:
Using @Brandon’s help, I’ve tried these approaches
<script type="text/javascript">
$(function () {
$("#Branches").change(function () {
var selected;
var url;
selected = $(this).val();
url = '@Url.Action("Index", "School")';
alert(url); //gives /School/Index/55 this is also my current page in my browser address bar
url = '@Url.Action("Index", "School", new {id = ""})';
alert(url); //gives /School
url = '@Url.Action("Index", "School", new {id = ""})' + '/' + selected;
alert(url); //gives /School/41
// window.location = url;
});
});
</script>
This is my route in global.asax just so you can see that I don’t have any crazy routes going on
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Update
This works:
url = '@Url.Action("NotIndex", "School", new {id = ""})' + '/' + selected;
I get the correct new Url and the action is hit with the selected ID
Just set
window.location.That should generate the URL to your action (clearing out the current ID) and then append the new route parameter.