开发者

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

开发者 https://www.devze.com 2023-04-01 00:09 出处:网络
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?

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?

  • 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.

on click of button ok i am calling

    $('a.comment').livequery("click", function (e) {
    var getpID = $(th开发者_如何学Cis).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?


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

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/

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

 });
0

精彩评论

暂无评论...
验证码 换一张
取 消