IsAjaxRequest()始终为false,部分视图不会重新加载

时间:2015-11-17 00:23:20

标签: c# jquery asp.net-mvc

我查看了所有其他stackoverflow帖子,但没有一个工作。

这是我的浏览页面中的位

@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>

这是我的包

bundles.Add(new ScriptBundle("~/bundles/shell").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery-ui-{version}.js",
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));

这是我的_Layout文件

@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>

我无法弄清楚出了什么问题。控制器很好,因为整个页面都使用searchString刷新,但是我无法获得id =“this”的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);
    }

0 个答案:

没有答案
相关问题