C# IsAjaxRequest()始终为false,部分视图将不会重新加载
我看过其他所有的stackoverflow帖子,但没有一个有用 这是我浏览页面中的一部分C# IsAjaxRequest()始终为false,部分视图将不会重新加载,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我看过其他所有的stackoverflow帖子,但没有一个有用 这是我浏览页面中的一部分 @using (Ajax.BeginForm("Browse", "Store", new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "this" })) { @Html.TextBox("searchString") <in
@using (Ajax.BeginForm("Browse", "Store",
new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "this"
}))
{
@Html.TextBox("searchString")
<input type="submit" value="Search"/>
}
<div id="this">
@Html.Partial("_Listings", Model)
</div>
这在我的布局文件中
@Scripts.Render("~/bundles/shell")
这是当我点击搜索按钮时应该刷新的部分视图
<div id="listings">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<div class="text-right">
<tr>
<td>
<h1>@Html.DisplayFor(modelItem => item.Title)</h1>
</td>
<td>
<h1> @Html.DisplayFor(modelItem => item.Description)</h1>
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td></td>
</tr>
</div>
}
</table>
</div>
您没有包含
jquery.unobtrusive ajax.js
文件,因此ajax.BeginForm
只是作为一个普通文件form@TejSoft,您可能会这么认为,但是如果它没有更新当前页面,那么它就不能更新。它只需要是if(Request.IsAjaxRequest()){…
@AlexHarley,不确定是什么原因导致它们无法加载。但可能只是注释掉@脚本。在布局中呈现(“~/bundles/shell”)
(以及您可能拥有的任何其他脚本),并在视图中添加jquery
和jquery。不引人注目的ajax
(在关闭
标记之前)测试。使用[HttpPost]装饰控制器,然后将ajax调用作为get请求是否与此有关?是否对if语句进行了评估?在控制器中放置一个断点,查看它是否被调用。
<div id="listings">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<div class="text-right">
<tr>
<td>
<h1>@Html.DisplayFor(modelItem => item.Title)</h1>
</td>
<td>
<h1> @Html.DisplayFor(modelItem => item.Description)</h1>
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td></td>
</tr>
</div>
}
</table>
</div>
[HttpPost]
public ActionResult Browse(string searchString)
{
var model = storeRepo.AllProducts(searchString).Take(10);
if (new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest())
{
Debug.WriteLine("IsAjax");
return PartialView("_Listings", model);
}
return View("Browse", model);
}