2

Am having trouble getting multiple parameters with Ajax in MVC. I have two fields that require an input. Input field for Username and CommentText.

I am defining these parameters in the url section of the ajax. It is working fine when I only pass one parameter(works for both when tried separately), but as soon as I try both the latter parameter does not work.

Ajax function:

$(function () {
    $("#button").click(function () {
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf8",
            url: "Home/AddComment?CommentText=" + $("#CommentText").val() + "&Username=" + $("Username").val(),
            dataType: "json",
            success: function (Comment) {
                //some function
            },
            error: function (xhr, err) {
                //some code
            }
        });
    });
});

Any ideas? Should I maybe be passing the parameters through "data" instead?

Edit: *This is the controller that should catch these parameters.*

 public JsonResult AddComment(string commentText, string username)
        {
            Comment c = new Comment() { CommentText = commentText, Username = username };
            CommentRepository.Instance.AddComment(c);
            return Json(GetComments(), JsonRequestBehavior.AllowGet);

        }
3
  • 1
    You should pass them not via url but as a data Commented Mar 27, 2014 at 11:01
  • 1
    Also, you will get an error here $("Username").val() don't forget to add # like $("#Username").val() Commented Mar 27, 2014 at 11:11
  • well the missing # was the error.. Why is it more preferable to pass it as data vs passing through the url? Commented Mar 27, 2014 at 11:29

3 Answers 3

3

You can use something like this:

Ajax

$.ajax({
    type: 'GET',
    url: 'Home/AddComment',
    data: { CommentText: $("#CommentText").val(), 
             Username: $("#Username").val() },
    cache: false,
    success: function (result) {            
        desc = result;
    }
});

And then in your controller:

public string AddComment(string CommentText, string Username)
{
    //your code here
}

Hope this will help you.

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

1 Comment

check the update.. I have a controller that takes in these parameters
0
 $(function () {
     $("#button").click(function () {
         $.ajax({
             type: "GET",
             contentType: "application/json; charset=utf8",
             url: "Home/AddComment",
              data: '{"CommentText":"' + $("#CommentText").val() + '", "Username":"' + $("Username").val() + '"}'
            dataType: "json",
             success: function (Comment) {
                 //some function
             },
            error: function (xhr, err) {
                 //some code
           }
        });
   });
});

Comments

0

you can move you all variables/parameters into one array, and then you can try the following.. and then you can read these array values in C#....

var val1=$("#componentName1").val();
var val2=$("#componentName2").val();
...
var parameterArray={val1,val2,val3....}
$.ajax({
         type: "GET",
         contentType: "application/json; charset=utf8",
         url: "Home/AddComment",
          data: parameterArray,
        dataType: "json",
         success: function (Comment) {
             //some function
         },
        error: function (xhr, err) {
             //some code
       }
    });

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.