javascript - JQuery 中带参数的重定向操作

标签 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, { operation: "edit", carId: thisRowId });
    }
};

//more code

查看(管理汽车):

@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>

Controller :

[HttpGet]
public ActionResult CarOperation(string operation, int carId)
{
    //some code...

    return RedirectToAction("ManagementCar", "Backoffice");
}

但是重定向后出现服务器错误,提示 carId 参数为空。我调试我的 jquery 并且该参数不为空。我也尝试过这样做

$.get(url, { operation: "edit", carId: thisRowId });

相反

window.location.replace(url, { operation: "edit", carId: thisRowId });

但它不重定向。 我该如何解决这个问题?

最佳答案

通过给它一个新值来设置它,如下所示。

window.location = window.location.replace(url, "shouldBeAstringNotAJsonObject");

关于javascript - JQuery 中带参数的重定向操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374911/

相关文章:

javascript - 根据属性值隐藏div?

php - HTML 表单未通过复选框

javascript - 如何访问从 PHP 收到的 ajax 响应中的数组元素?

javascript - 防止在嵌套的 .NET 母版页上验证失败后清除元素

asp.net - ASP.NET MailKit SMTP响应

javascript - 反向循环真的更快吗?

javascript - 类型错误 : d is not a function in jsPlumb

javascript - 如何将 toAlternatingCase 自定义方法添加到 JavaScript 中的 String 类?

javascript - 使用 jquery 创建我自己的图片库网格

javascript - 如何在 asp 中的剑道 UI 网格字段中编写链接按钮的单击函数