JQUERY ajax将值从MVC View传递给Controller

cjB*_*not 30 ajax model-view-controller asp.net-mvc jquery

我想要的是将View中的txtComments值(使用jquery/ajax)传递给Controller.

问题是ajax/jquery不接受脚本标记为字符串.这意味着,当我在txtComments中输入任何脚本/ html标签时,ajax会进入错误功能而无法进入控制器.

这是jQuery:

        $('#btnSaveComments').click(function () {
            var comments = $('#txtComments').val();
            var selectedId = $('#hdnSelectedId').val();

            $.ajax({
                url: '<%: Url.Action("SaveComments")%>?id=' + selectedId + '&comments=' + escape(comments),
                type: "post",
                cache: false,
                success: function (savingStatus) {
                    $("#hdnOrigComments").val($('#txtComments').val());
                    $('#lblCommentsNotification').text(savingStatus);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#lblCommentsNotification').text("Error encountered while saving the comments.");
                }
            });
        });
Run Code Online (Sandbox Code Playgroud)

这是控制器:

        [HttpPost]
        public ActionResult SaveComments(int id, string comments){
             var actions = new Actions(User.Identity.Name);
             var status = actions.SaveComments(id, comments);
             return Content(status);
        }
Run Code Online (Sandbox Code Playgroud)

我也试过$('#txtComments').serialize()而不是逃避(评论),但仍然是一样的.

tob*_*s86 53

尝试使用data$.ajax功能的选项.更多信息在这里.

$('#btnSaveComments').click(function () {
    var comments = $('#txtComments').val();
    var selectedId = $('#hdnSelectedId').val();

    $.ajax({
        url: '<%: Url.Action("SaveComments")%>',
        data: { 'id' : selectedId, 'comments' : comments },
        type: "post",
        cache: false,
        success: function (savingStatus) {
            $("#hdnOrigComments").val($('#txtComments').val());
            $('#lblCommentsNotification').text(savingStatus);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#lblCommentsNotification').text("Error encountered while saving the comments.");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


小智 7

这是执行相同调用的另一种方法.你的类型应该总是在CAPS中,例如.键入:"GET"/类型:"POST".

$.ajax({
      url:/ControllerName/ActionName,
      data: "id=" + Id + "&param2=" + param2,
      type: "GET",
      success: function(data){
            // code here
      },
      error: function(passParams){
           // code here
      }
});
Run Code Online (Sandbox Code Playgroud)

另一种方法是在链接上使用data-ajax.

<a href="/ControllerName/ActionName/" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#_content">Click Me!</a>
Run Code Online (Sandbox Code Playgroud)

假设你有一个带有我__tent的div,这将调用该动作并用该动作返回的数据替换该div中的内容.

<div id="_content"></div>
Run Code Online (Sandbox Code Playgroud)

不是你的问题的直接答案,但它应该知道的一些信息;).


小智 5

$('#btnSaveComments').click(function () {
    var comments = $('#txtComments').val();
    var selectedId = $('#hdnSelectedId').val();

    $.ajax({
        url: '<%: Url.Action("SaveComments")%>',
        data: { 'id' : selectedId, 'comments' : comments },
        type: "post",
        cache: false,
        success: function (savingStatu`enter code here`s) {
            $("#hdnOrigComments").val($('#txtComments').val());
            $('#lblCommentsNotification').text(savingStatus);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#lblCommentsNotification').text("Error encountered while saving the comments.");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)