【问题标题】:Redirect with Ajax POST使用 Ajax POST 重定向
【发布时间】:2014-08-07 08:49:28
【问题描述】:

我是 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 方法运行成功,但不是将我重定向到 MaSelection 视图,而是返回我调用该方法的第一个视图,所以我尝试在我的 ajax 方法中放置一个“成功”片段,然后我将位置替换为“马选择”视图,但我知道视图丢失了 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);
    }

【问题讨论】:

  • 你不能只传递 selectedId 和你要重定向到的目标 url 吗?

标签: c# javascript ajax asp.net-mvc-5


【解决方案1】:

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

你也需要这个;

Convert object to JSON string in C#

【讨论】:

  • 这不是数据类型的问题,我的操作获取了 id 并返回了项目列表,我尝试了这个,但它给了我一些错误
【解决方案2】:

如果你想要重定向页面,在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>

JS

$(".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) {
            $("#updateTargetId").html(msg);
        },
        error: function (xhr) {
            alert("something seems wrong");
        }
    });
});

CONTROLLER(返回 PartialView)

[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);
}

【讨论】:

  • 这里是场景,我调用了将项目列表返回给视图 MaSelection 的 MaselectionAction,当我将返回视图与 selectionOffre 列表放在一起时,它可以正常工作,但是在动作成功,它返回到 ajax 并检查成功片段,当我输入 window.location.href = '@Url.Action("MaSelectionOffre", "DemandeLocation")';视图丢失选定列表并返回“找不到资源”。
【解决方案3】:

我将我的操作更改为获取操作,并在我的按钮中添加了带有链接和 ID 的 window.location.replace

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2011-04-16
    相关资源
    最近更新 更多