我正在观看多元化网站上的Scott Allen视频,我正在谈论这个module:
AJAX :
所以我创建了与视频中显示的完全相同的示例,但它不起作用,我不知道为什么花了一个小时寻找这个,这是我的代码:
At BoundleConfig.cs file:
bundles.Add(new ScriptBundle("~/bundles/otf").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-{version}.js",
"~/Scripts/jquery.unobtrusive-ajax.min.js",
"~/Scripts/jquery.validate*"
));
在appSettings部分的Web.config文件中,我添加了:
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
我们走了:
public ActionResult Review(string searchString)
{
var model = _repository.StartDs;
if (!String.IsNullOrEmpty(searchString))
{
model = model.Where(x => x.FirstName.Contains(searchString));
}
if (Request.IsAjaxRequest())
{
return PartialView("_Review", model);
}
return View(model);
}
Review.cshtml文件:
@model IEnumerable<nauka.Models.StartData>
@using(Ajax.BeginForm(
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "girlsList"
}))
{
<input type="search" name="searchString"/>
<input type="submit" value="Znajdź" />
}
@Html.Partial("_Review", Model)
_Review.cshtml文件:
@model IEnumerable<nauka.Models.StartData>
<div id="girlsList">
<table style="width:100%">
<tr>
<th>ID</th>
<th>Imię</th>
<th>Nazwisko</th>
<th>Wzrost</th>
<th>Rozmiar</th>
<th>Akcje</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.FirstName</td>
<td>@item.LastName</td>
<td>@item.Height</td>
<td>@item.TitsSize</td>
<td>@Html.ActionLink("Usuń", "Delete", new { id = item.Id })</td>
</tr>
}
</table>
</div>
我用我的动作方法检查了这个片段:
if (Request.IsAjaxRequest())
{
return PartialView("_Review", model);
}
从不执行,这意味着请求不被识别为AjaxRequest,问题是为什么?我编辑了这一行并粘贴了
return View("Index");
所以它将我重定向到我的索引站点,但它没有,所以我证明了AjaxRequest没有被执行。我不知道,这里缺少什么,我完全按照所显示的视频完成。
在我的_Layout.cshtml文件上方,自动添加到每个视图页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("Dodaj", "Add", "Home")</li>
<li>@Html.ActionLink("Przegladaj", "Review", "Home")</li>
<li>@Html.ActionLink("Filtruj", "FilterTest", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/otf")
@RenderSection("scripts", required: false)
</body>
</html>
答案 0 :(得分:0)
Ough ...解决了!!
我没有添加
通过NuGet包安装程序将jquery.unobtrusive-ajax.min.js 库添加到我的解决方案中。 但是当我之前将这个添加到BoudleConfig(路径 &#34;〜/ Scripts / jquery.unobtrusive-ajax.min.js&#34;,),为什么它没有喊出这个库在我的Visual?项目中没有出现! 它造成了许多复杂情况。