CD *_*ith 2 jquery asp.net-mvc-3
我需要根据MVC 3中下拉列表中值的选择重新加载我的页面.我的下拉列表定义如下:
@Html.DropDownListFor(model => model.ID, new SelectList(Model.SchoolBranches, "ID", "Name", Model.ID), new { id = "Branches", name = "Branches"})
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的脚本定义如下:
<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>
Run Code Online (Sandbox Code Playgroud)
我选择的ID工作正常,因为警报会在更改时显示正确的ID.只是找不到任何显示如何导航回索引操作并发送新ID的示例.我找到的所有样本都使用ajax显示部分页面或此类刷新.我需要重新加载整个页面.
谢谢
更新:
使用@ Brandon的帮助,我尝试了这些方法
<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>
Run Code Online (Sandbox Code Playgroud)
这是我在global.asax中的路线,所以你可以看到我没有任何疯狂的路线
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
);
}
Run Code Online (Sandbox Code Playgroud)
更新
这有效:
url = '@Url.Action("NotIndex", "School", new {id = ""})' + '/' + selected;
Run Code Online (Sandbox Code Playgroud)
我获得了正确的新Url,并使用所选ID点击了该操作
刚设置window.location.
window.location = '@Url.Action("Index", "Controller", new { id = "" })' + '/' + selected;
Run Code Online (Sandbox Code Playgroud)
这应生成您的操作的URL(清除当前ID),然后附加新的路由参数.
| 归档时间: |
|
| 查看次数: |
8360 次 |
| 最近记录: |