Điều tôi muốn là chuyển giá trị của txtComments từ Chế độ xem (sử dụng jquery/ajax) sang Bộ điều khiển.JQUERY giá trị truyền ajax từ MVC Xem tới Bộ điều khiển
Sự cố là ajax/jquery không chấp nhận thẻ tập lệnh dưới dạng chuỗi. Có nghĩa là, khi tôi nhập bất kỳ thẻ script/html nào trong txtComments, ajax sẽ chuyển sang chức năng lỗi và không thể đi vào bộ điều khiển.
Đây là 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.");
}
});
});
Dưới đây là bộ điều khiển:
[HttpPost]
public ActionResult SaveComments(int id, string comments){
var actions = new Actions(User.Identity.Name);
var status = actions.SaveComments(id, comments);
return Content(status);
}
Tôi cũng đã cố gắng $('#txtComments').serialize()
thay vì thoát (bình luận) nhưng vẫn như nhau.
tôi đang đối mặt với cùng một vấn đề! bạn đã giải quyết nó như thế nào? – moji