0

I have a stored procedure that selects all the fields in the table based on the date. I then created a method shown below to return the results as JSON.

[HttpGet]
public JsonResult GetResult()
        {
            MonthNameConverter converter = new MonthNameConverter();
            string fullDate = converter.startOfMonth().ToShortDateString();
            string[] split = fullDate.Split('/');
            string date = "";
            if(Convert.ToInt32(split[0]) < 10)
            {
                date = split[2] + "-0" + split[0];
            }
            else
            {
                date = split[2] + "-" + split[0];
            }            
            var results = travelSOCC.GetLansingMileage(date).ToList();
            return Json(results, JsonRequestBehavior.AllowGet);
        }

However when I go to append the data to an HTML table I'm getting an unidentified result.

$(function LoadData() {
            $("#LansingTable tbody tr").remove();
            $.ajax({
                type: 'GET',
                url: '@Url.Action("GetResult")',
                dataType: 'json',
                data: JSON,
                success: function (data) {
                    $.each(data, function (item) {
                        var rows = "<tr><td>" + item.TravelDate + "</td><td>" + item.TripType + "</td></tr>";
                        $("#LansingTable tbody").append(rows);
                    });
                },
                error: function (ex) {
                    var r = jQuery.parseJSON(response.resonseText);
                    alert("Message: " + r.Message);
                }
            })
        });

Any help is greatly appreciated.

5
  • Is GetResult a POST method ? Commented May 26, 2020 at 17:09
  • Your action method is get but your ajax reguest is post.Default http verb for action method is get. Commented May 26, 2020 at 17:13
  • it was missing the [httpPost] but I added that and I am still receiving the same issue. Commented May 26, 2020 at 17:14
  • Can you elaborate on "unidentified result"? Commented May 26, 2020 at 17:16
  • @Crowcoder I added more information in the original post, but basically its saying item.TravelDate is undefined, which I don't understand why that would be the case. Commented May 26, 2020 at 17:29

1 Answer 1

1

Please modify $.each(data, function(item) { as below:

$.each(data, function(idx, item) {

Please refer documentation here for more information.

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

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.