2
votes

I have an action link which I want to do an HTTP Post to my Controller, however, I keep getting a HTTP 500.

Here is my jQuery

<script type="text/javascript">

    $(document).ready(function () {
        $('.thing').click(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.href,
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data.Success == true) {
                       // do something
                    }
                    else {
                        alert(data.Message);
                    }
                },
                error: function (textStatus, errorThrown) {
                    // request always errors
                }
            });
        });
    });    
</script>

and my Action link code

@Html.ActionLink("my link", "DoStuff", "Dude", new { thingId = item.Id }, new { @class = "thing" })

My Controller

public class DudeController : Controller
{
    [HttpPost]
    public ActionResult DoStuff(int thingId)
    {
        return Json(new { Success = true, Message = string.Empty }, JsonRequestBehavior.AllowGet);
    }

}

Any ideas?

2
your controller is expecting an integer value and you are sending none in your ajax call??Rafay
@3nigma The @Html.ActionLink code generates the url /somewhere/Dude/DoStuff/2 where 2 is my thingId integer in my controller ... I'm trying to just send a http post to that url. Am I misunderstanding something here?Adam
o yup!! i didnt see it earlier sry...Rafay
i would first use fiddler and to see what the request is. I would then remove the HttpPost from DoStuff method and try accessing the url the ActionLink produces.Alexandros B
@Circadian - Post looks good in Fiddler, found the problem though. Was a crappy job on my part, had an exception in one of the controller's dependencies.Adam

2 Answers

0
votes

Can you try to remove dateType from ajax call and add data? For me it works:

$(document).ready(function () {
$('.thing').click(function (e) {
    e.preventDefault();
    $.ajax({
        url: this.href,
        type: "POST",
        data: this.thingId,
        success: function (data) {
            if (data.Success == true) {
                // do something
                alert(data.Message);
            }
            else {
                alert(data.Message);
            }
        },
        error: function (textStatus, errorThrown) {
            // request always errors
        }
    });
});
});
0
votes

For anyone that encounters this issue, here's another potential point of interest. In my controller I had the following:

    var events = (from e in db.EventMsts                                                      
    where e.Eventid.Equals(eventID)
    orderby e.EventNm
    select new { e.Eventid, e.EventNm, e.EventDescription, e.URLEventSite, e.URLTicketSales,e.EventDates  })
                                    .Take(1)
                                    .ToArray();
    return Json(events);

Which was not throwing any exception, and then the browser would receive a HTTP 500.

e.EventDates was a collection of objects while the others were strings/integers. Simply removing e.eventDates from the controller (and the ajax request) got everything to be successful (aside from missing the data I want).

Per Craig's response there are security limitations that prevents passing back arguments like that (Craig I'm just trusting you on this since I've not actually seen a doc regarding this). The solution was to tear my EventDates object down to a single delimited string so that I can parse it in jQuery. Here's what my code became and now works for me - note an event has event Dates, and an event date has event times.

    var dateTimeList = new StringBuilder();

    var times = (from t in db.EventTimes
          where t.EventDate.Eventid.Equals(eventID)
                             orderby t.EventStartTm
                             select
                                new {
                                        t.EventDate.EventDt,
                                        t.EventStartTm,
                                        t.EventEndTm}).ToArray();
    foreach (var time in times)
    {
        if (dateTimeList.Length > 0)
        {
            dateTimeList.Append("|" + time.EventDt + " from " + time.EventStartTm + " to " + time.EventEndTm ?? "whenever");
        }
        else
        {
            dateTimeList.Append(time.EventDt + " from " + time.EventStartTm + " to " + time.EventEndTm ?? "whenever");
        }
    }
    var dateTimeString = dateTimeList.ToString();
    var evnt = (from e in db.EventMsts
                where e.Eventid.Equals(eventID)
                orderby e.EventNm
        select new { e.Eventid, e.EventNm, e.EventDescription, e.URLEventSite, e.URLTicketSales, dateTimeString })
        .FirstOrDefault();

    return Json(evnt);