Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.Ajax with MVC

I am trying to use jQuery ajax to save the value that the user entered in the Textbox to the database. But I am struck how to proceed. What I did so far:

User clicks button and I call jQuery function and am calling the controller

comments = $("#txtComments").val();
var request = $.ajax({
                url: "/Home/SaveCommentsData",
                type: "POST",
                data: { comment: comments },
                dataType: "json"
            });

and I am not sure how to get this comment value in the controller and send a value back to jQuery on success.

like image 436
user2067567 Avatar asked Dec 04 '22 12:12

user2067567


1 Answers

try data like this

data :{'comment':comments}

and use the same variable as string type in controller action

comments = $("#txtComments").val();
var request = $.ajax({
                url: "/Home/SaveCommentsData",
                type: "POST",
                data: { 'comment': comments },
                dataType: "json"
            });

Controller

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveCommentsData( string comment)
        {

//
}

Regards

like image 51
Sridhar Narasimhan Avatar answered Dec 26 '22 03:12

Sridhar Narasimhan