简体   繁体   中英

Asp.Net MVC Partial View Update with Ajax

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.

Try adding this to your view file.

@{
    Layout = null;
}

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM