最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redirect action with parameters in JQuery - Stack Overflow

programmeradmin1浏览0评论

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:

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

$.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?

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:

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

$.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?

Share Improve this question edited Jun 1, 2020 at 13:05 Ninita asked Dec 4, 2013 at 12:05 NinitaNinita 1,2492 gold badges22 silver badges49 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 0

set it by giving it a new value like this.

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

The problem with using window.location is 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().

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.

I have put together a jQuery.navigate plugin that 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 editItem function altogether and simply change your markup to something like this?

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

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

My RouteConfig:

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

My 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

发布评论

评论列表(0)

  1. 暂无评论