C# 使用ajaxpost重定向

C# 使用ajaxpost重定向,c#,javascript,ajax,asp.net-mvc-5,C#,Javascript,Ajax,Asp.net Mvc 5,我是ajax新手,我正在尝试从ajax方法调用post操作 $(".buttonSelection").click(function () { selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value'); $.ajax({ // Call MaSelection action method url: "/D

我是ajax新手,我正在尝试从ajax方法调用post操作

 $(".buttonSelection").click(function () {

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
    $.ajax({
        // Call MaSelection action method
        url: "/DemandeLocation/MaSelectionOffre",
        data: { id: selectedId },
        type: 'Post',
        success: function (msg) {
            window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre")');
        },
        error: function (xhr) {
            alert("something seems wrong");
        }
    });
    });
我的post方法使用success,但它没有将我重定向到MaSelection视图,而是返回我调用该方法的第一个视图,因此我尝试在我的ajax方法中放入一个“success”片段,并将位置替换为“Ma selection”视图,但我知道该视图丢失了id,因此它变为null,我如何使用ajax实现它

这里是我的帖子,了解更多细节

[HttpPost]
    [Authorize(Roles = "Locataire")]
    public ActionResult MaSelectionOffre(string id)

    {
        int DemandeLocationGetbyId = Convert.ToInt32(id);

        var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList();

        return View("MaSelectionOffre", selectionOffre);
    }

使用
json
作为数据类型

 $(".buttonSelection").click(function () {

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
    $.ajax({
        // Call MaSelection action method
        url: "/DemandeLocation/MaSelectionOffre",
        dataType:"json",
        data: { id: selectedId },
        type: 'Post',
        success: function (msg) {
            window.location.href = msg.redirect;
        },
        error: function (xhr) {
            alert("something seems wrong");
        }
    });
    });
你也需要这个


如果您想要重定向页面,在ajax调用之后,您应该使用

...
success: function (msg) {
    window.location.href = '@Url.Action("MaSelectionOffre", "DemandeLocation")';
},
...

编辑

如果要替换结果,请使用类似以下内容:

HTML

<div id="updateTargetId">
    //table
        //tr
            //td
                //your button that has cssClass buttonSelection
</div>
控制器(返回部分视图)


我将我的操作更改为get操作,并在按钮中添加了window.location.replace和link和ID

<button type="button" class="buttonSelection" onclick="window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre?id="+item.Publication_ID)')"> <span class="ui-icon ui-icon-cart"></span> </button>


难道你不能将selectedId和你要重定向到的目标url一起传递吗?数据类型没有问题,我的操作获取id并返回项目列表,我尝试了这一操作,但它给了我一些似乎不正确的东西这是一个场景,我调用MaSelection操作,将项目列表返回到视图MaSelection,问题是,当我使用selectionOffre列表放置retrun视图时,它的工作方式与我想要的一样好,但是在操作成功后,当我放置window.location.href='@Url.action(“MaSelectionOffre”,“DemandLocation”)'时,它返回到ajax并检查成功片段;查看丢失的选定列表并返回“找不到资源”。
[HttpPost]
[Authorize(Roles = "Locataire")]
public ActionResult MaSelectionOffre(string id)

{
    int DemandeLocationGetbyId = Convert.ToInt32(id);

    var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList();

    return PartialView("MaSelectionOffre", selectionOffre);
}
<button type="button" class="buttonSelection" onclick="window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre?id="+item.Publication_ID)')"> <span class="ui-icon ui-icon-cart"></span> </button>