繁体   English   中英

尝试使用Jquery帖子和ActionResult刷新页面

[英]Trying to refresh page using Jquery post and ActionResult

我一直在尝试使“ RedirecttoAction”语句在这里起作用,但它不会让步。 方法如下:

    [HttpPost]
    public ActionResult UpdateTech(int tech, int id)
    {
        Callout callout = db.Callouts.Find(id);
        callout.TechnicianId = tech;
        callout.TechStatus = "ASSIGNED";
        db.Entry(callout).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("CalloutAdmin/Index/");
    }

它由下面的JQuery调用,它可以完美地工作:

$(function () {
        $(".techdropdown").change(function () {
            var recordToUpdate = $(this).attr("id");
            var selectedTech = $(".techdropdown option:selected").val();
            window.alert(selectedTech);
            $.post("/CalloutAdmin/UpdateTech?tech=" + selectedTech + "&id=" + recordToUpdate);
        });
    });

唯一没有发生的是刷新页面,这是重定向应该执行的操作。 有人可以建议吗?

更新

我正在尝试做的是更新此Callout对象,而不重定向到另一个视图。 我从下拉列表中选择一个技术人员,然后它将标注的技术人员值自动设置为我从下拉菜单中选择的值。

您正在将重定向返回到AJAX请求,并且AJAX请求正在后台加载页面。 如果要使整个页面重定向,则可能要在jQuery中使用成功回调

$.post("/CalloutAdmin/UpdateTech?tech=" + selectedTech + "&id=" + recordToUpdate, function() { document.location = "CalloutAdmin/Index/"; } );

如果要让操作控制重定向的位置,则可能要使用从操作中返回的JsonResultContentResult以及从其中输出的具有jQuery的成功回调重定向用户的URL。

假设UpdateTech和Index都属于CallOutAdminController,然后将UpdateTech方法更改为:

[HttpPost]
public ActionResult UpdateTech(int tech, int id)
{
    Callout callout = db.Callouts.Find(id);
    callout.TechnicianId = tech;
    callout.TechStatus = "ASSIGNED";
    db.Entry(callout).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index","CallOutAdmin");
}

然后使您的jQuery函数如下所示:

$(document).ready(function(e) {
    $(".techdropdown").change(function () {
        var recordToUpdate = $(this).attr("id");
        var selectedTech = $(".techdropdown option:selected").val();
        window.alert(selectedTech);
        window.location = "/CallOutAdmin/UpdateTech?tech=" + selectedTech + "&id=" + recordToUpdate
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM