1

I have an asp.net mvc application. I need to reload a partial with ajax post. Here is my cshtml code inside main view:

<button data-url='@Url.Action("GetErrors", "Interface", new { servers = "S97EGESRV01" })'
            class="js-reload-details">
        Reload
    </button>
    <div id="errorListDiv">

        @Html.Partial("~/Views/Partials/_ErrorListPartial.cshtml", Model.Errors)
    </div>

javascript:

$('.js-reload-details').on('click', function (evt) {
    evt.preventDefault();
    evt.stopPropagation();

    var errorListDiv = $('#errorListDiv');
    var url = $(this).data("Interface/GetErrors?servers=S97EGESRV01");

    $.get(url, function (data) {
        errorListDiv.replaceWith(data);
    });
});

C#:

public ActionResult HatalariGetir(string servers= "All", InterfaceViewModel interfaceModel = null)
{
if (interfaceModel == null) {
     interfaceModel = new InterfaceViewModel();
}
// Some database processes
List<ErrorViewModel> modelList = new List<ErrorViewModel>();
// Populating modelList
interfaceModel.Errors = modelList;
return View("~/Views/Partials/_ErrorListPartial.cshtml", interfaceModel.Errors);
}

But the thing is when I click "Reload" button, whole page is loaded inside errorListDiv. Not just partial view. What am I doing wrong?

EDIT: I tried this:

return PartialView("~/Views/Partials/_ErrorListPartial.cshtml", interfaceModel.Errors);

Unfortunately result is same.

Umut Derbentoğlu
  • 1,146
  • 5
  • 18
  • 39

2 Answers2

2

Try adding this to your view file.

@{
    Layout = null;
}
Matt
  • 3,638
  • 2
  • 26
  • 33
1

Actually your partial view is not working like partial view, it is rendering with master layout as a full view, so what you need is to explicitly tell in view that do not use any master layout by adding this line in top of view:

@{


Layout = null;

}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160