0

I'm trying to do a simple action with some JavaScript code. I've got some items on a scheduler (DevExpress scheduler component). When I'm double clicking on an appointment (an item then), it should raise an JS function which is the case. My function should get the selected appointment id and pass it to Controller Action. Here is my JS code :

function DoubleClick() {
        debugger;
        var apt = GetSelectedAppointment(scheduler);
        var aptid = apt.appointmentId;
        $.ajax({
            type: "POST",
            url: "/Home/GetAppId",
            data: { id: aptid },
            dataType: 'json',
            success: function () {
                alert("ok");
            },
            error: function () {
                alert("error");
            }
        });
    }

And here is my C# code :

[HttpPost]
public JsonResult GetAppId(int id)
{
    context = new SchedulingDataClassesDataContext();

    DBAppointment app = (from a in context.DBAppointment where a.UniqueID == id select a).FirstOrDefault();
    return Json(new {rep = app});
}

As you can see in my JS code, I'm not doing anything special in case of success. However, I never reach the success part. Plus, when I'm looking at the Chrome dev tool (F12), I'm getting that red error message.

POST http://localhost:25206/Home/GetAppId 500 (Internal Server Error)

Anything that I'm doing wrong?

Traffy
  • 2,803
  • 15
  • 52
  • 78

4 Answers4

0

Man, you need to force things as follows

 return Json(new {rep = app},"text/json",JsonRequestBehavior.AllowGet);

In addition, mind your navigation properties (if any) in order to avoid circular reference

Bellash
  • 7,560
  • 6
  • 53
  • 86
0

Please remove the name of the property in ajax data and edit that property as below.

function DoubleClick() {
    debugger;
    var apt = GetSelectedAppointment(scheduler);
    var aptid = apt.appointmentId;
    $.ajax({
        type: "POST",
        url: "/Home/GetAppId",
        data: aptid,
        dataType: 'json',
        success: function () {
            alert("ok");
        },
        error: function () {
            alert("error");
        }
    });
}

and edit your controller as follows

[HttpPost]
public JsonResult GetAppId([FromBody]int id)
{
    //...
}

Please read this blog post which is a good read and allowed me to understand what's going on.

http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

The original question that I asked

Simple post to Web Api

Community
  • 1
  • 1
Amila
  • 3,711
  • 3
  • 26
  • 42
0

According to your error your problem somewhere in select your data from DB or creating anonymous object when you try to serialize it to Json. I rewrite your select to simplify it and not creating any anonymous objects when return it from Controller like this:

[HttpPost]
public JsonResult GetAppId(int id)
{
    context = new SchedulingDataClassesDataContext();

    DBAppointment app = context.DBAppointment.FirstOrDefault(x => x.UniqueID == id);
    return Json(app);
}

Does it work like this?

teo van kot
  • 12,350
  • 10
  • 38
  • 70
-1

Try changing the last line of the method to:

return Json(new { rep = app }, JsonRequestBehavior.AllowGet);

You need to tell C# to allow the json to be returned to the client.

histr0n
  • 29
  • 2
  • 1
    The method is marked with `[HttpPost]` and the ajax call specifies `type: "POST",` so this is completely necessary –  Mar 10 '15 at 10:13