jQuery.Ajax与MVC

use*_*567 4 asp.net-mvc jquery

我正在尝试使用jQuery ajax将用户在文本框中输入的值保存到数据库中.但我很震惊如何继续.到目前为止我做了什么:

用户点击按钮,我调用jQuery函数,并调用控制器

comments = $("#txtComments").val();
var request = $.ajax({
                url: "/Home/SaveCommentsData",
                type: "POST",
                data: { comment: comments },
                dataType: "json"
            });
Run Code Online (Sandbox Code Playgroud)

并且我不确定如何在控制器中获取此注释值并在成功时将值发送回jQuery.

Sri*_*han 6

试试这样的数据

数据:{'评论':评论}

并在控制器操作中使用与字符串类型相同的变量

comments = $("#txtComments").val();
var request = $.ajax({
                url: "/Home/SaveCommentsData",
                type: "POST",
                data: { 'comment': comments },
                dataType: "json"
            });
Run Code Online (Sandbox Code Playgroud)

调节器

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveCommentsData( string comment)
        {

//
}
Run Code Online (Sandbox Code Playgroud)

问候


Ali*_*hşi 6

脚本

$.ajax({
   url: "/Home/SaveCommentsData",
   type: "POST",
   data: { comment: comments },
   dataType: "json",
   success: function (data) {  
       // data is returning value from controller
       // use this value any where like following
       $("#div_comment").html(data);
   }
});
Run Code Online (Sandbox Code Playgroud)

调节器

[HttpPost]
public ActionResult SaveCommentsData(string comment)
{
    // save comment
    var result = someData; // maybe saved comment
    return Json(result);
}
Run Code Online (Sandbox Code Playgroud)