gpt4 book ai didi

c# - Request.IsAjaxRequest() 总是返回 false

转载 作者:行者123 更新时间:2023-12-01 05:45:33 24 4
gpt4 key购买 nike

Request.IsAjaxRequest() 始终返回 false 。我已经多次看到这个问题,通常包括 jquery.unobtrusive-ajax.min.js 修复了这个问题,但无法弄清楚出了什么问题。我已将 jquery.unobtrusive-ajax.min.js 包含在我的脚本包中,但我似乎找不到任何错误来解释为什么它返回 false。我设置了一个断点,它每次都会跳过 if(Request.IsAjaxRequest())

这是我的代码

家庭 Controller

       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());
}

}
}

我的索引 View

            @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"
));

最佳答案

您是否也在 web.config 中启用了不显眼的 javascript?

<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

Request.IsAjaxRequest() 函数检查 http header “X-Requested-With”:如果值为“XMLhttprequest”,则返回 true,否则返回 false。

关于c# - Request.IsAjaxRequest() 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27077955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com