-1

I have an ajax call:

$.ajax({
         url: '@Url.Action("LoadCategory", "Home")',
         type: 'GET',
         contentType: "application/json; charset=utf-8",
         data: { fstValue: modelDDLValue },
         success: function (result) {
           // do something here
        });

and action method in Home Controller:

public JsonResult LoadCategory(string fstValue)
 {
      int modelId = 0;
        if (fstValue != "")
            modelId = Convert.ToInt32(fstValue);

        var result = unitOfWork.CategoryRepository.GetCategoriesForModel(modelId);

        string listUL = "<ul class=\"root\">";

        IList<SelectListItem> Data = new List<SelectListItem>();
        foreach (Category cat in result)
        {
            listUL += "<li><a class='parent' href=\"javascript:void()\">" + cat.CategoryNameEng + "<span class=\"value\">" + cat.Id.ToString() + "</span></a>";
            Data.Add(new SelectListItem()
            {
                Text = cat.CategoryNameEng,
                Value = cat.Id.ToString(),
            });

            if (cat.SubCategories.Count() > 0)
            {
                listUL += "<ul class=\"l1\">";
                foreach (SubCategory scat in cat.SubCategories.Where(s => s.ModelId == modelId).ToList())
                {
                    listUL += "<li><a class='sub' href=\"javascript:void()\">" + scat.SubCategoryNameEng + "<span class=\"value\">" + "S" + scat.Id.ToString() + "</span></a></li>";
                    Data.Add(new SelectListItem()
                    {
                        Text = scat.SubCategoryNameEng,
                        Value = "S" + scat.Id.ToString(),
                    });
                }
                listUL += "</ul>";
            }
            listUL += "</li>";
        }
        listUL += "</ul>";

        // add listUL into Data as the last element
        Data.Add(new SelectListItem() { Value = "listUL", Text = listUL });

     return Json(Data, JsonRequestBehavior.AllowGet); 
 }

But on webhost I'm always getting an error "failed to load resource the server responded with a status of 500 (internal server error)".

I tried to replace ajax method with $.get() and other things, read all answers on this topic in stackoverflow but no luck.

How do I handle with this? Thanks a lot in advance!

Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45
  • Your action method might get hit. Set a break point and check it. The AJAX call might not be the problem. Instead, please debug the piece of code not provided here, i.e. `// here some logic`. – Andrei V Oct 02 '14 at 08:17
  • 500 error is often the result of an exception being thrown in your controller method. –  Oct 02 '14 at 08:18
  • the code runs well on debug mode, the problem arises only on webhost after deploying it – Dmitry Stepanov Oct 02 '14 at 08:19
  • 1
    Add the [Elmah.MVC package](https://www.nuget.org/packages/Elmah.MVC/), then check the logs next time the error occurs. – Tieson T. Oct 02 '14 at 08:29
  • @Tieson T., I installed Elmach, redeploy the project, where can I find the log? – Dmitry Stepanov Oct 02 '14 at 08:38
  • As noted in [linked project site](https://github.com/alexanderbeletsky/elmah-mvc): /elmah – Tieson T. Oct 02 '14 at 08:39
  • @ Andrey V, I've updated the controller method, but once again this method runs well on debug, but fails in webhost – Dmitry Stepanov Oct 03 '14 at 06:34

1 Answers1

0

The problem was with this error: There is already an open DataReader associated with this Command which must be closed first

The solution was found here: There is already an open DataReader associated with this Command which must be closed first

Adding to the connection string MultipleActiveResultSets=true solved the issue.

Community
  • 1
  • 1
Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45