1

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

2 Answers 2

5

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) 
{
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Darin Dimitrov! I have the same code for another action but its not working can you please help me out. please find my code -- stackoverflow.com/questions/7344877/…
1

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

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

 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.