【发布时间】:2012-01-19 03:28:01
【问题描述】:
我想要的是将 txtComments 的值从视图(使用 jquery/ajax)传递给控制器。
问题是 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(cmets) 但还是一样。
【问题讨论】:
-
我也面临同样的问题!你是怎么解决的?
标签: jquery ajax asp.net-mvc model-view-controller