Bhargav
Bhargav

Reputation: 451

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. how can i do that?

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 = $(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?

Upvotes: 1

Views: 11197

Answers (2)

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

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

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

 });

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

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

Upvotes: 5

Related Questions