In ASP.NET MVC judged by Request.IsAjaxRequest () if you want to load public view

Author: @nele
This article original author, please indicate the source: https://www.cnblogs.com/nele/p/4974840.html


table of Contents

 

Individual visual Request.IsAjaxRequest () this thing is to determine the header foreground filed over the X-Requested-With: XMLHttpRequest is not to distinguish between ajax requests.

 

ASP.NET MVC3 we can specify "_layout.cshtml" Page Layout as we defined in the "_ViewStart.cshtml", and of course, we can also in the "_ViewStart.cshtml" loaded in a different layout as needed. _ViewStart.cshtml I define public view, including the header and footer.

      I would like to address is if it is carried out with jquery AJAX requests, no need to load a common view.

Copy the code
<script type="text/javascript">
    $(function () {
        $('#theLink').click(function () {
            $.ajax({
                url: $(this).attr('href'),
                type: "GET",
                success: function (response) {
                    $('#mainContent').html(response);
                }
            });
            return false;
        });
    });
</script>
Copy the code

As follows:

In ~ / Views / ViewStart.cshtml in

Copy the code
@{
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml";
}
在 controller:

public ActionResult Index()
{
    return View();
}          
Copy the code

Guess you like

Origin www.cnblogs.com/Jeely/p/10951253.html