3

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.

4
  • 1
    SaveCommentsData is name of the action in the controller?What MVC are you using? Commented Feb 25, 2013 at 8:42
  • what does the browser console say? is there a error? Commented Feb 25, 2013 at 8:45
  • @yechie yes itz a action . MVC4 Commented Feb 25, 2013 at 8:51
  • Your could have sent request just like 'comments='+comments just as a plain string too.Does your browser console shows data goin to the server? Commented Feb 25, 2013 at 8:55

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

what if i want to send multiple texts in data: { comment: comments } ?
6

script

$.ajax({
   url: "/Home/SaveCommentsData",
   type: "POST",
   data: { comment: comments },
   dataType: "json",
   success: function (data) {  
       // data is returning value from controller
       // use this value any where like following
       $("#div_comment").html(data);
   }
});

controller

[HttpPost]
public ActionResult SaveCommentsData(string comment)
{
    // save comment
    var result = someData; // maybe saved comment
    return Json(result);
}

Comments

2

client side script-jQuery

$.ajax({
   url: "/Home/SaveCommentsData",
   type: "post",
   data: { comment: comments },
   dataType: "application/json",
   success: function (data) {  
       if(data.Success)
       {
          alert('Done');
       }
   }
});

controller side code

[HttpPost]
public ActionResult SaveCommentsData(string comment)
{
    // save comment
    return Json(new {Success:true});
}

1 Comment

Thanks. i tried and working fine. But success funciton alert not working ?
2

try this

comments = $("#txtComments").val();
var request = $.ajax({
    url: '@Url.Action("SaveCommentsData","Home")',
    type: "POST",
    data: JSON.stringyfy({ 'comment': comments }),
    dataType: "json",
    success: function(data){
      alert(data.status);
    }
});

Controller

[HttpPost]
public JsonResult SaveCommentsData(string comment)
{
   //Do something
   return Json(new
            {
                status = false
            });
}

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.