我想要的是将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()而不是逃避(评论),但仍然是一样的.