1

jquery ajaxを使用してリクエストを処理すると、コントローラーでnull値が取得されます

コントローラ

[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 PartialView("CustomizedQuestions");
        }

Jquery-Ajax コード

 var test = "{ answers: '" + $("#answerlist").val() + "', question: '" + title + "', controlid: '" + controlid + "', eventid: '" + eventid + "' }";
                        $.ajax({
                            url: '@Url.Action("UpdateAnswers")',                                
                            type: 'POST',
                            dataType: 'html',
                            contentType: 'application/html; charset=utf-8',
                            context: $(this),
                            // data: "{ answers: '"+$("#answerlist").val()+"' ,question: '"+ title +"', controlid:'"+ controlid +"',eventid:'"+ eventid+"'}",
                             data: JSON.stringify(test),   
                            success: function (result) {
                                $(this).dialog("close");
                            },
                            error: function () {
                                //xhr, ajaxOptions, thrownError
                               alert('there was a problem saving the new answers, please try again');
                            }
                        });
4

1 に答える 1

4

あなたcontentTypeは間違っています。application/htmlJSONを渡すときになぜに設定したのですか? このようにしてみてください:

var test = { answers: $('#answerlist').val(), question: title, controlid: controlid, eventid: eventid };
$.ajax({
    url: '@Url.Action("UpdateAnswers")',                                
    type: 'POST',
    dataType: 'html',
    contentType: 'application/json; charset=utf-8',
    context: $(this),
    data: JSON.stringify(test),   
    success: function (result) {
        $(this).dialog("close");
    },
    error: function () {
        //xhr, ajaxOptions, thrownError
        alert('there was a problem saving the new answers, please try again');
    }
});

またはapplication/x-www-form-urlencodedデフォルトのどちらを使用します:

var test = { answers: $('#answerlist').val(), question: title, controlid: controlid, eventid: eventid };
$.ajax({
    url: '@Url.Action("UpdateAnswers")',                                
    type: 'POST',
    dataType: 'html',
    context: $(this),
    data: test,
    success: function (result) {
        $(this).dialog('close');
    },
    error: function () {
        //xhr, ajaxOptions, thrownError
        alert('there was a problem saving the new answers, please try again');
    }
});
于 2012-05-24T11:13:18.060 に答える