3

I am using JQuery Ajax to make a simple call to an ASP.NET MVC controller. Here is my code

   function postdata(idno) {
   console.log(idno);

   $.ajax({
        type: 'POST',
        url: "/IM/GetMessages",
        contentType: 'application/json',
        dataType: 'json',            
        data: JSON.stringify({ 'fUserId': idno }), 
        success: function (data) { /*CODE*/

    });}

The controller looks like this

     [HttpPost]
     public ActionResult GetMessages(decimal? fUserId)
     {
        var list = WebUtility.IMMessages.Where(p =>
            (p.ToUserId == Session.UserId  && (!fUserId.HasValue || fUserId.HasValue && p.User == fUserId.Value)))
            .OrderBy(p => p.CreatedDateTime)
            .Select(p => new { MessageId = p.RecordId, MessageBody = p.Description1 });

        return Json(list, JsonRequestBehavior.AllowGet);
     }

The problem is that my data doesn't pass to my controller, "null" passes instead. how can I correct this issue? I am watching "idno" on console and everything seems to be OK.

2
  • Try looking at the network tab of your browser to see what is being sent in the request, and try changing the parameter to a non nullable decimal (though I wouldn't think that would matter). Commented Dec 10, 2013 at 14:20
  • On network tab, everything seemed to be ok, actually now the code i gave you is working now, obviously i focused on wrong part of my code. Thanks for your that much quick answer, and sorry for my that much late response. Commented Dec 27, 2013 at 13:11

4 Answers 4

1

A quick search found a lot of materials on Stack Overflow relating to this issue. Pulled from this answer, try changing this:

data: JSON.stringify({ 'fUserId': idno }),

to this:

data: "fUserId=" + JSON.stringify(idno),
Sign up to request clarification or add additional context in comments.

Comments

1

There is no reason to convert a single parameter into a JSON if you ask me. Instead just do this:

$.ajax({
    type: 'POST',
    url: "/IM/GetMessages?fUserId=" + idno,
    dataType: 'json',            
    success: function (data) { /*CODE*/
});

This way you can still get back JSON but you pass single parameter value. Now if you really need to send an object I don't see anything wrong with your code. You might want to declare a javascript variable and turn it into a json object like this:

var myVar = { fUserId: idno };

and then use that in your ajax request:

$.ajax({
    type: 'POST',
    url: "/IM/GetMessages",
    contentType: 'application/json',
    dataType: 'json',            
    data: JSON.stringify(myVar), 
    success: function (data) { /*CODE*/

});

I do this daily and it works fine for me with both nullable and non-nullable types...

1 Comment

I am really glad that you have given a detailed answer. You were right that there was nothing wrong with my code, it is working now, actually i don't know what the real problem was.
0

You should just be able to do this:

data: { fUserId: idno }

Comments

0

I changed nothing with my code, but it is working now. I think there was another problem affecting passing data. I changed many things generally in my code and now actually i don't know what the problem exactly was. Maybe the solution was just rebooting my web application and clearing all caches and cookies in web browser.

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.