1

我使用 jquery ajax 调用控制器操作(请参见下面的代码),执行操作后,引发了 Invalid Json 错误。我注意到,它返回页面内容。为什么会发生。谁能帮我解决这个问题?

控制器

[HttpGet]
       public ActionResult ViewDetails(int id)
       {
           var eventsdetails = _service.GeteventByID(id);
           return View("EventDetails",eventsdetails);
       }


       [HttpPost]
       public ActionResult UpdateAnswers(string answers, string question, string controlid, int eventid)
       {
           var replacetext=string.Empty;
           if (answers.Length>0) 
             replacetext = answers.Replace("\n", ",");
           _service.UpdateAnswers(eventid, replacetext, controlid);
          return RedirectToAction("ViewDetails", new { id = eventid });
       }

Javascript

function dropdownlist(controlid, title, answers, eventid) {
            var $answersreplaced = answers.replace( /\,/g , " \r");
            var $deleteDialog = $('<div><textarea id="answerlist"  rows="10" cols="50">' + $answersreplaced + '</textarea><div><div style="font-size:9px">(To change back to an open answer field, delete all choices above and save)</div>');
            $deleteDialog.dialog({
                resizable: false,
                height: 280,
                width: 350,
                title: title + " - Edit Choices",
                modal: true,
                buttons: {
                    "Save": function () {
                        $.ajax({
                            type: 'POST',
                            url: '@Url.Action("UpdateAnswers")',
                            dataType: 'json',
                            data: { answers: $('#answerlist').val(),
                                question: title,
                                controlid: controlid,
                                eventid: eventid
                            },
                            success: function (result) {
                                $(this).dialog("close");
                                alert(result);
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert(thrownError);
                                // alert('there was a problem saving the new answers, please try again');
                            }
                        });
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });
        };
4

2 回答 2

1

这是因为您没有从操作返回 JsonResult,而是重定向到另一个操作,该操作又返回 ViewResult。

尝试这样的事情:

[HttpPost]
public ActionResult UpdateAnswers(string answers, string question, string controlid, int eventid)
{
    var replacetext=string.Empty;
    if (answers.Length>0) 
        replacetext = answers.Replace("\n", ",");
    _service.UpdateAnswers(eventid, replacetext, controlid);
    var eventsdetails = _service.GeteventByID(eventid);
    return Json(eventdetails);
}
于 2012-05-23T11:42:14.550 回答
0

dataType: 'json'在 AJAX 请求和UpdateAnswers控制器操作中指定您return RedirectToAction显然重定向到 ViewDetails 操作,该操作又返回一个视图,该视图又是 HTML。所以这没有意义。

您必须从 UpdateAnswers 控制器操作中返回 Json 而不是重定向。

于 2012-05-23T11:43:37.403 回答