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.