Javascript 在JQuery中使用参数重定向操作
在我的ASP.Net项目中,我试图在选择行表并单击按钮后将页面重定向到其他操作。所以我有这个代码: JQuery:Javascript 在JQuery中使用参数重定向操作,javascript,jquery,asp.net,redirect,Javascript,Jquery,Asp.net,Redirect,在我的ASP.Net项目中,我试图在选择行表并单击按钮后将页面重定向到其他操作。所以我有这个代码: JQuery: function editItem(tableId, url) { if ($("#" + tableId + " .selected").exists()) { var thisRowId = $("#" + tableId + " .selected").attr("id"); window.location.replace(url,
function editItem(tableId, url) {
if ($("#" + tableId + " .selected").exists()) {
var thisRowId = $("#" + tableId + " .selected").attr("id");
window.location.replace(url, { operation: "edit", carId: thisRowId });
}
};
//more code
[HttpGet]
public ActionResult CarOperation(string operation, int carId)
{
//some code...
return RedirectToAction("ManagementCar", "Backoffice");
}
routes.MapRoute(
name: "ManipulatingCar",
url: "{controller}/{action}/{operation}/{carId}"
);
editItem = function (tableId, url) {
if ($("#" + tableId + " .selected").exists()) {
var thisRowId = $("#" + tableId + " .selected").attr("id");
var fullUrl = url + "/edit/" + thisRowId;
window.location = fullUrl;
}
};
视图(管理车):
@model myProject.Model.Car.Car[]
<table class="table" id="tableCars">
<thead>
@*some code to header*@
</thead>
<tbody>
foreach (var item in Model)
{
<tr id="@(item.Id)" onclick="selectRow('tableCars', '@(item.Id)')">
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.OwnerName)
</td>
<td>
@(item.IsSold == true ? "Yes" : "No")
</td>
</tr>
}
</tbody>
</table>
<br />
<button id="btEdit" type="button" class="disable" onclick="editItem('tableCars','CarOperation')">Edit</button>
但重定向后出现服务器错误,称carId
参数为空。我调试jquery,并且该参数不为null。我也试着去做
$.get(url, { operation: "edit", carId: thisRowId });
反而
window.location.replace(url, { operation: "edit", carId: thisRowId });
但它不会重定向。
如何解决这个问题?通过给它一个新值来设置它,如下所示
window.location = window.location.replace(url, "shouldBeAstringNotAJsonObject");
好的,我终于用一个新的url路由配置和以下代码解决了这个问题: 我的路线图:
function editItem(tableId, url) {
if ($("#" + tableId + " .selected").exists()) {
var thisRowId = $("#" + tableId + " .selected").attr("id");
window.location.replace(url, { operation: "edit", carId: thisRowId });
}
};
//more code
[HttpGet]
public ActionResult CarOperation(string operation, int carId)
{
//some code...
return RedirectToAction("ManagementCar", "Backoffice");
}
routes.MapRoute(
name: "ManipulatingCar",
url: "{controller}/{action}/{operation}/{carId}"
);
editItem = function (tableId, url) {
if ($("#" + tableId + " .selected").exists()) {
var thisRowId = $("#" + tableId + " .selected").attr("id");
var fullUrl = url + "/edit/" + thisRowId;
window.location = fullUrl;
}
};
我的JQuery:
function editItem(tableId, url) {
if ($("#" + tableId + " .selected").exists()) {
var thisRowId = $("#" + tableId + " .selected").attr("id");
window.location.replace(url, { operation: "edit", carId: thisRowId });
}
};
//more code
[HttpGet]
public ActionResult CarOperation(string operation, int carId)
{
//some code...
return RedirectToAction("ManagementCar", "Backoffice");
}
routes.MapRoute(
name: "ManipulatingCar",
url: "{controller}/{action}/{operation}/{carId}"
);
editItem = function (tableId, url) {
if ($("#" + tableId + " .selected").exists()) {
var thisRowId = $("#" + tableId + " .selected").attr("id");
var fullUrl = url + "/edit/" + thisRowId;
window.location = fullUrl;
}
};
基本上,控制器动作参数必须与
RouteConfig.cs
中指定的配置匹配使用窗口位置的问题在于请求时没有传递引用者,因为这种行为只是模仿新请求,就好像您在地址栏中键入了URL一样。如果你打算使用网站分析,一个可靠的推荐人将是相当重要的。我使用jQuery生成一个动态链接,在该链接上调用click()
对不起,实际上我弄错了。它将我重定向到一个未定义的页面,如:https://localhost/myProject/Backoffice/undefined
。我不明白为什么…?javascript替换函数使用2个字符串作为参数。您正在第二个参数上传递一个json对象。我的意思是,您不应该在replace函数上传递json对象。replace(url,“shouldBeAstringNotAJsonObject”);我尝试使用一个字符串,但我得到了这个错误:“参数字典包含一个空条目”(…)