[英]Ajax.BeginForm() not working asynchronously
我正在嘗試學習ASP.Net MVC,但我無法使Ajax.BeginForm異步更新部分視圖。 這是操作視圖中的代碼:
@using (Ajax.BeginForm(
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "restaurantList"
}))
{
<input type="search" name="searchTerm" />
<input type="submit" value="Search By Name." />
}
@Html.Partial("_Restaurants", Model)
操作中對ajax請求的檢查始終返回false,並且整個屏幕都會更新,而不是部分視圖會更新。 看起來像這樣:
if (Request.IsAjaxRequest())
{
return PartialView("_Restaurants", model);
}
return View(model);
我在BundleConfig中包含了一堆jquery腳本文件。
bundles.Add(new ScriptBundle("~/bundles/otf").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-{version}.js",
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*",
"~/Scripts/jquery-migrate-{version}.js",));
我在項目中所有視圖所使用的共享布局視圖的主體部分結尾之前調用render函數
@Scripts.Render("~/bundles/otf")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
我已經嘗試解決了2天,但我一無所獲。 任何幫助將不勝感激!
確保Scripts
文件夾中存在jquery.unobtrusive-ajax.js
腳本,並在<script>
標記中提取該<script>
。
如果不是這樣,請確保已在項目中安裝了Microsoft.jQuery.Unobtrusive.Ajax
NuGet,它將把此腳本添加到應用程序的Scripts
文件夾中。
我已經注意到,使用Visual Studio 2015創建新的ASP.NET MVC應用程序時,此腳本不是現成的,我必須安裝此NuGet才能使用它。 我不知道在最新的MVC版本中刪除此腳本的原因-也許它已被棄用,但現在有另一種方法可以實現該目的,但是距離我進行MVC開發已經很長時間了,但我還不是很清楚。確定現在推薦的方法是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.