Request.IsAjaxRequest()
始终返回false
。我已经看过很多次这样的问题了,通常包括jquery.unobtrusive-ajax.min.js
修复了这个问题,但是无法弄清楚出了什么问题。我在我的脚本包中包含了jquery.unobtrusive-ajax.min.js
,我似乎无法找到错误的原因。我输入了一个断点,每次跳过if(Request.IsAjaxRequest())
这是我的代码
家庭控制器
public ActionResult Index(string sortOrder)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
using (var db = new ActorMovieDB())
{
var actors = from A in db.Actors
select A;
switch (sortOrder)
{
case "name_desc":
actors = actors.OrderByDescending(A => A.ActorName);
break;
case "Date":
actors = actors.OrderBy(A => A.DateOfBirth);
break;
case "awardsWon":
actors = actors.OrderByDescending(A => A.AwardsWon);
break;
default:
actors = actors.OrderBy(A => A.ActorName );
break;
}
if (Request.IsAjaxRequest())
{
return PartialView("_Actors", actors.ToList());
}
else
{
return View(actors.ToList());
}
}
}
我的索引视图
@Ajax.ActionLink("Awards Won","index",new { sortOrder = "Date" },
new AjaxOptions
{
HttpMethod = "Get",
UpdateTargetId="actorsdiv",
InsertionMode = InsertionMode.Replace},
new { @class = "btn btn-info" })
@Html.Partial("_Actors",Model)
<div class="jumbotron" id="actorsdiv" style="margin:100px;width:1150px">
@foreach (var item in Model)
{
<ul>
<li>
<img src="@item.ImageUrl" width="150" height="150" style="margin- />
<h4>Name: @item.ActorName</h4>
<p>Date of Birth: @item.DateOfBirth.ToShortDateString()</p>
<p>Awards Won: @item.AwardsWon</p>
</li>
</ul>
}
</div>
捆绑配置
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/toastr.js",
"~/Scripts/bootstrap.js"
));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.unobtrusive-ajax.min.js"
));
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new StyleBundle("~/Content/css")
.Include("~/Content/bootstrap.css", "~/Content/toastr.css"
));
答案 0 :(得分:0)
您是否也在web.config中启用了不显眼的javascript?
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
Request.IsAjaxRequest()函数检查http标头&#34; X-Requested-With&#34;:如果值为&#34; XMLhttprequest&#34;它返回true,否则返回false。