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.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
huxyz
  • 31
  • 1
  • 3
  • 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). – Jason P Dec 10 '13 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. – huxyz Dec 27 '13 at 13:11

4 Answers4

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),
Community
  • 1
  • 1
Zach
  • 437
  • 3
  • 10
  • 27
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...

Marko
  • 12,543
  • 10
  • 48
  • 58
  • 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. – huxyz Dec 27 '13 at 14:11
0

You should just be able to do this:

data: { fUserId: idno }
James
  • 2,195
  • 1
  • 19
  • 22
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.

huxyz
  • 31
  • 1
  • 3