我想做的是从视图(使用jquery / ajax)将txtComments的值传递给控制器。
问题是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.");
}
});
});
这是控制器:
[HttpPost]
public ActionResult SaveComments(int id, string comments){
var actions = new Actions(User.Identity.Name);
var status = actions.SaveComments(id, comments);
return Content(status);
}
我也尝试过使用$('#txtComments').serialize()
代替escape(comments),但结果还是一样。