javascript 如何在asp.net mvc3中使用jquery函数将值从视图传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7228877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:23:43  来源:igfitidea点击:

How to pass values from view to controller using jquery function in asp.net mvc3

javascriptjqueryasp.net-mvc-3

提问by Bhargav

I have to pass values from view to controller, controller having action method that will call webmethod to set values into database. how can i do that?

我必须将值从视图传递给控制器​​,控制器具有调用 webmethod 将值设置到数据库中的操作方法。我怎样才能做到这一点?

  • need not to create a view of using model.
  • 不需要创建使用模型的视图。

I have a view that get the values from database and having one link that is comment. on click of comment one textarea box will open and after providing some input. will click on ok button.

我有一个从数据库中获取值的视图,并且有一个链接是评论。单击评论后,将打开一个 textarea 框,并在提供一些输入后。将点击确定按钮。

on click of button ok i am calling

单击按钮确定我正在打电话

    $('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();
    });
}

now what can i do to get getpId and comment_text values to the controllers SetComment action?

现在我能做些什么来获取控制器 SetComment 操作的 getpId 和 comment_text 值?

回答by Darin Dimitrov

You could use $.postmethod to send them as an AJAX request:

您可以使用$.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
});

which will post to the following action:

这将发布到以下操作:

public ActionResult SetComment(string commentText, string postId) 
{
    ...
}

回答by Jayantha Lal Sirisena

you can use jquery post http://api.jquery.com/jQuery.post/

你可以使用 jquery post http://api.jquery.com/jQuery.post/

$.post("/Home/SetComment",{comment_text:Text,postId:PostId},function(data){

 });