简体   繁体   English

如何在ASP.NET MVC3中使用jQuery函数将值从视图传递到控制器

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

I have to pass values from view to controller, controller having action method that will call webmethod to set values into database. 我必须将值从视图传递到控制器,该控制器具有操作方法,该方法将调用webmethod将值设置为数据库。 how can i do that? 我怎样才能做到这一点?

  • 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. 单击评论后,将打开一个textarea框,并提供一些输入。 will click on ok button. 将单击确定按钮。

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? 现在我该怎么做才能将getpId和comment_text值获取到控制器的SetComment操作?

You could use $.post method 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) 
{
    ...
}

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){

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM