-3

Ajax Call

<div id="partialSummaryDiv">@{Html.RenderPartial("CalendarPartialView");}</div>
<script type="text/javascript">
    function SearchByMonth()
    {
        var x = document.getElementById("MonthInputField");
        if (x.value != "") {
            $.ajax({
                url: '/Employees/SearchWholeMonth',
                type: 'GET',
                data: { "month": x.value, "id": @y },
                error: function (data) {
                    alert('Error!');
                },
                success: function (data) {
                    $('#partialSummaryDiv').html(data);
                    alert("succes");
                }
            });
        }
        else {
            alert('Date not selected!')
        }
    }
</script>

Controller

[HttpGet]
public PartialViewResult SearchWholeMonth(DateTime month,int id)
{
   ...calculations
   return PartialView("CalendarPartialView", TotalList);
}

So i have this ajax call and the controler with the partial view. I ran the controller code with breakpoints and the calculations and returned results are correct. The problem is that after i press the buttos there are no results,no errors. In the developer console on network the request has code 200 for OK and there are also no errors there. The ajax function does not enter succes and nothing is shown in the end. Please help! Ty

In the partial view i also have @model List<float> and i am showing the results like this

@String.Format("{0} {1}", @i, @Model[j])

EDIT!!!!!!!!!!!!!!!!!!!!: The ajax/view/controller works fine but the partial view is not rendering on the details page! I have tried to render it on a different page and it works there. How can i make it render on the Details page!

xDevil
  • 147
  • 1
  • 2
  • 12
  • You should return Json in the controller. Ajax can't handle a PartialView result – Maria Ines Parnisari Dec 15 '15 at 13:14
  • i have used the same method for another view and it is displaing that one correctly – xDevil Dec 15 '15 at 13:18
  • Your browser has probably cached a previous, incomplete result from the server. Try accessing the URL directly. If the data returned by that URL is going to change frequently, you may need to switch to POST, or else use $.ajaxSetup({ cache: false }); – Dave Dec 15 '15 at 14:31
  • i did put cache:false and tried a diferent id that i have not used but it still did not work... – xDevil Dec 15 '15 at 14:32
  • On button click, are you entering the SearchByMonth function? – gyosifov Dec 15 '15 at 14:33
  • it is displaying the result if i access the url directly but it does not show it in the partial view – xDevil Dec 15 '15 at 14:34
  • yes it is entering the function and the view with the right results – xDevil Dec 15 '15 at 14:35
  • What jQuery are you using? After 1.8 the .success and .error methods are deprecated. And .done and .fail are the recommended use. http://api.jquery.com/jquery.ajax/ I doubt this to be the problem, but it is worth the try :) – gyosifov Dec 15 '15 at 14:38
  • i am using jquerry 1.10 – xDevil Dec 15 '15 at 14:40
  • Ok, try using $ajax(...).always(function() { alert( "complete" ); }); to see if the error is jQuery related or not – gyosifov Dec 15 '15 at 14:43
  • i tried using the always function and the alert did not trigger – xDevil Dec 15 '15 at 14:45
  • unless you provide an example solution which we can debug, we are not going to be able to help you! I've tried it a couple of times and it works under IE8, chome, FF – noobed Dec 15 '15 at 15:50

2 Answers2

0

Try to get your callbacks using the Http Status Code, like this:

         $.ajax({
                url: '/Employees/SearchWholeMonth',
                type: 'GET',
                data: { "month": x.value, "id": @y },
                statusCode: {
                  200: function (data) { 
                          $('#partialSummaryDiv').html(data);
                          alert("succes");
                  },
                  500: function (erro) { alert(erro); }
                 }
            });
Pedro Benevides
  • 1,970
  • 18
  • 19
-1

I had to delete the controller and the views and then it just worked! Thank you all who tried to help!

xDevil
  • 147
  • 1
  • 2
  • 12