استفاده از pjax بجای ajax در ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۳/۰۳ ۱۲:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<title>@ViewBag.Title</title> @RenderBody()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.pjax.js"></script>
<script type="text/javascript">
$(function () {
$(document).pjax('a[withpjax]', '#pjaxContainer', { timeout: 5000 });
});
</script>
</head>
<body>
<div>Main layout ...</div>
<div id="pjaxContainer">
@RenderBody()
</div>
</body>
</html> @{
if (Request.Headers["X-PJAX"] != null)
{
Layout = "~/Views/Shared/_PjaxLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
} using System.Web.Mvc;
namespace PajxMvcApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}
} @{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.ActionLink(linkText: "About", actionName:"About", routeValues: null,
controllerName:"Home", htmlAttributes: new { withpjax = "with-pjax"})
@Html.ActionLink(item.Name, item.ActionName, item.ControllerName,
new { Id = item.Id, area = item.AreaName }, new { @class = "drop", withpjax = "with-pjax" }) //The new api
$(document).pjax('a[withpjax]', '#pjax-container')
//Which is roughly the same as
$(document).on('click', 'a[withpjax]', function(event) {
$.pjax.click(event, '#pjaxContainer')
})
$(document).pjax('a[withpjax]', '#pjaxContainer', { timeout: 5000 }); $(document).pjax('a', '#pjaxContainer', { timeout: 5000 }); <input type="submit" value="Save" />
@Ajax.beginform(...)
<div id="pjaxContainer"> @RenderBody() </div>
و اینم خروجی یک Save
$(document).on('pjax:send', function() {
$('#loading').show()
})
$(document).on('pjax:complete', function() {
$('#loading').hide()
}) @section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<div id="main">
@RenderBody()
</div>
//**********
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("Scripts", required: false)
<script type="text/javascript">
$(function () {
$(document).pjax('a[withpjax]', '#main', { timeout: 5000 }); @Html.ActionLink("ارتباط با ما","Contact", "Home"
, null,new { withpjax="with-pjax" }) @RenderSection("Scripts", false)