dav*_*317 0 javascript asp.net-mvc jquery razor
我有以下代码在第一次使用时有效:
$("#CompDD").change(function () {
//var parts = (window.location.pathname.split("/"));
var ctrlName = '@ViewContext.RouteData.Values["Controller"].ToString()';
var actnName = '@ViewContext.RouteData.Values["Action"].ToString()';
var url = (ctrlName + "/" + actnName + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
//delete ctrlName;
// delete actnName;
//window.location = ($(location).attr('href') + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
//window.location = (ctrlName + "/" + actnName + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
//$(location).attr('href', url);
window.location.href = url;
//alert(ctrlName + "\n" + actnName);
});
Run Code Online (Sandbox Code Playgroud)
但是,在随后对相关下拉菜单(#CompDD)进行更改时,它将在链接末尾添加另一个控制器/操作,例如。它在现有“顾客/索引”的末尾添加另一个“顾客/索引”,thenit添加searchsrting变量等。
请原谅我代码中的注释和内容。我如何让Jquery(或javascript)重定向,而无需一遍又一遍地添加控制器名称和动作名称,或者什么是最好的方法?
编辑:如此简单的修复!我必须将根斜杠添加到URL字符串,此示例有效:
var url = ("/" + ctrlName + "/" + actnName + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
Run Code Online (Sandbox Code Playgroud)
注意我构造的字符串开头的正斜杠...。
使用Url.Action辅助方法来构建操作方法的路径。
$("#CompDD").change(function () {
var baseUrl="@Url.Action("Home","Search")";
alert(baseUrl);
// Now append your query string variables to baseUrl
// Ex : baseUrl=baseUrl+"?searchString=testing";
window.location.href=baseUrl;
});
Run Code Online (Sandbox Code Playgroud)
假设您要导航到控制器中的search操作方法Home。
| 归档时间: |
|
| 查看次数: |
6838 次 |
| 最近记录: |