JQuery Success函数没有触发

时间:2014-07-25 21:49:57

标签: javascript jquery ajax asp.net-mvc

我有以下脚本。它运行,它将变量传递给控制器​​,控制器正确执行,但无论出于何种原因,成功函数不会触发,因此不会刷新我的html。相反,错误会触发。关于原因,没有什么可以向我跳出来。谢谢你的帮助!

$(function() {
    $("#btnUpdateTick").unbind('click').click(function () {
        var currenttick =
            {
                "TicketID":@Html.Raw(Json.Encode(Model.TicketID)),
                "Title": $("#Title").val(),
                "Creator": $("#Creator").val(),
                "StatusID": $("#StatusID").val(),
                "Description": $("#Description").val(),
                "newComment":$("#txtAddComment").val(),
                Cat:
                    {
                        "CatID":$("#ddCurrTickCat").val()
                    }
            }
        //var newcomment = $("#txtAddComment").val();
        var conv = JSON.stringify(currenttick);
        $.ajaxSetup({cache:false});
        $.ajax({
            url: '@Url.Action("UpdateTicket", "HelpDesk")',
            data: JSON.stringify({ticket:currenttick}),
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                $("#loadpartial").html(data);
            },
            error: function (data){alert("turd")}
        });
    });
});

我的控制器:

   [HttpPost]
    public PartialViewResult UpdateTicket(Tickets ticket)
    {

        ////Tickets.UpdateTicket(currenttick);

        if (ticket.newComment != "")
        {

            Comments.addCommentToTicket(ticket.TicketID, ticket.newComment,UserPrincipal.Current.SamAccountName.ToString());
        }
        Tickets model = new Tickets();

        ViewBag.CategoryList = Category.GetCategories();
        ViewBag.StatusList = TicketStatus.GetStatusList();

        model = Tickets.GetTicketByID(ticket.TicketID);
        model.TicketComments = new List<Comments>();
        model.TicketComments = Comments.GetCommentsForTicketByID(ticket.TicketID);

        //model.TicketComments = Comments.GetCommentsForTicketByID(ticketID);

        //ViewBag.TicketComments = Comments.GetCommentsForTicketByID(ticketID);

        return PartialView("TicketDetails", model);
    }

2 个答案:

答案 0 :(得分:0)

您的控制器正在返回视图,而不是json。您应该返回JsonResult。试试这个:

[HttpPost]
public JsonResult UpdateTicket(Tickets ticket)
{

    ////Tickets.UpdateTicket(currenttick);

    if (ticket.newComment != "")
    {

        Comments.addCommentToTicket(ticket.TicketID, ticket.newComment,UserPrincipal.Current.SamAccountName.ToString());
    }
    Tickets model = new Tickets();

    ViewBag.CategoryList = Category.GetCategories();
    ViewBag.StatusList = TicketStatus.GetStatusList();

    model = Tickets.GetTicketByID(ticket.TicketID);
    model.TicketComments = new List<Comments>();
    model.TicketComments = Comments.GetCommentsForTicketByID(ticket.TicketID);

    //model.TicketComments = Comments.GetCommentsForTicketByID(ticketID);

    //ViewBag.TicketComments = Comments.GetCommentsForTicketByID(ticketID);

    return Json(model);
}

答案 1 :(得分:0)

如果您想从ajax调用返回部分视图,请将您的ajax请求修改为:

 $.ajax({
        url: '@Url.Action("UpdateTicket", "HelpDesk")',
        data: JSON.stringify({ticket:currenttick}),
        type: "POST",
        dataType: "html",
        success: function (data) {
            $("#loadpartial").html(data);
        },
        error: function (data){alert("turd")}
    });

现在,ur success函数中的“data”将从PartialViewResult()返回html。

相关问题