我必须将值从视图传递给控制器,控制器具有动作方法,该方法将调用webmethod将值设置为数据库。我怎么能这样做?
我有一个视图,从数据库中获取值,并有一个注释链接。点击评论一个textarea框将打开并提供一些输入后。将点击“确定”按钮。
点击按钮确定我正在呼叫
$('a.comment').livequery("click", function (e) {
var getpID = $(this).parent().attr('id').replace('commentBox-', '');
var comment_text = $("#commentMark-" + getpID).val();
//alert(getpID);
//alert(comment_text);
if (comment_text != "Write a comment...") {
//$.post("/Home/SetComment?comment_text=" + comment_text + "&post_id-= " + getpID, {
}, function (response) {
$('#CommentPosted' + getpID).append($(response).fadeIn('slow'));
// $("#commentMark-" + getpID).val("Write a comment...");
$("#commentMark-" + getpID).val();
});
}
现在我该怎么做才能将getpId和comment_text值赋给控制器SetComment动作?
答案 0 :(得分:5)
您可以使用$.post
方法将它们作为AJAX请求发送:
var url = '@Url.Action("SetComment", "Home")';
var data = { commentText: comment_text, postId: getpID };
$.post(url, data, function(result) {
// TODO: do something with the response from the controller action
});
将发布到以下操作:
public ActionResult SetComment(string commentText, string postId)
{
...
}
答案 1 :(得分:1)
你可以使用jquery post http://api.jquery.com/jQuery.post/
$.post("/Home/SetComment",{comment_text:Text,postId:PostId},function(data){
});