javascript 在 JQuery 中使用参数重定向操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20374911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:21:05  来源:igfitidea点击:

Redirect action with parameters in JQuery

javascriptjqueryasp.netredirect

提问by Ninita

In my ASP.Net project I'm trying to redirect a page to other action after select a row table and click on a button. So I have this code:

在我的 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

View (ManagementCar):

查看(管理车):

@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");
        }

But I have a Server Error after redirect saying carId parameter is null. I debug my jquery and that parameter isn't null. I tried also doing

但是在重定向说 carId 参数为空后我有一个服务器错误。我调试我的 jquery 并且该参数不为空。我也试过

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

instead

反而

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

but it don't redirect. How can I solve this?

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

采纳答案by Ninita

Ok, I finally solved the problem with a new url routing config and the following code:

好的,我终于用一个新的 url 路由配置和以下代码解决了这个问题:

My RouteConfig:

我的路由配置:

routes.MapRoute(
            name: "ManipulatingCar",
            url: "{controller}/{action}/{operation}/{carId}"
        );

My JQuery:

我的 JQuery:

editItem = function (tableId, url) {
    if ($("#" + tableId + " .selected").exists()) {

        var thisRowId = $("#" + tableId + " .selected").attr("id");

        var fullUrl = url + "/edit/" + thisRowId;

        window.location = fullUrl;
    }
};

Basically, controller action parameters must match with the configurations specified in RouteConfig.cs

基本上,控制器动作参数必须与 RouteConfig.cs 中指定的配置相匹配

回答by Alvin

set it by giving it a new value like this.

通过给它一个像这样的新值来设置它。

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

回答by Michael Papworth

The problem with using window.locationis that the referrer is not passed on the request as this behaviour simply mimics a new request as if you had typed the URL into the address bar. If you intend to use website analytics, a reliable referrer will be quite important. I use jQuery to generate a dynamic link upon which I call click().

使用的问题window.location是引用者没有在请求中传递,因为这种行为只是模仿一个新的请求,就好像您在地址栏中输入了 URL 一样。如果您打算使用网站分析,可靠的推荐人将非常重要。我使用 jQuery 生成一个动态链接,我在该链接上调用click().

var url = '/my/url';
$('<a href="' + url + '"></a>')[0].click();

Notice I click()the underlying element not the jQuery selected object, the reason being that a jQuery click only raises the event and the href is not navigated to whereas indexing to the element and clicking that will cause the same behaviour you would expect as if you had actually clicked a link on the page.

请注意click(),底层元素不是 jQuery 选择的对象,原因是 jQuery 单击只会引发事件,而不会导航到 href ,而索引到元素并单击会导致与您期望的行为相同的行为,就好像您实际上有单击页面上的链接。

I have put together a jQuery.navigate pluginthat neatly wraps this up and abstracts your site map away from your UI logic, which you might find useful. For example, using my plugin would mean you could remove your editItemfunction altogether and simply change your markup to something like this?

我已经组合了一个jQuery.navigate 插件,它巧妙地包装了它并将您的站点地图从您的 UI 逻辑中抽象出来,您可能会发现它很有用。例如,使用我的插件意味着您可以editItem完全删除您的功能并简单地将您的标记更改为这样的内容?

<tr id="@(item.Id)" onclick="$.navigate('to', 'edit', { id : '@(item.Id)' })">