1

我需要做的就是创建一个简单的搜索页面,在部分视图中显示结果。我有一个搜索文本框和一个搜索提交按钮。我遵循了我在网上找到的一个教程,该教程似乎非常容易实现,但我在这里遗漏了一些东西。当我单击搜索按钮时,没有任何反应。任何关于我做错或遗漏的想法将不胜感激。

我在主布局页面中包含以下脚本文件

    @Script("jquery-1.5.1.min.js")
    @Script("modernizr-1.7.min.js")
    @Script("jquery-ui.min.js")
    @Script("jquery.unobtrusive-ajax.js")
    @Script("jquery.validate.min.js")
    @Script("jquery.validate.unobtrusive.min.js")

    @helper Script(string scriptName)
    {
        <script src="@Url.Content("~/Scripts/" + scriptName)" type="text/javascript">    </script>
    }

主搜索视图称为 AdminMenu。这列在我的项目中名为 Admin 的区域下。

在此处输入图像描述

我的主视图中的以下代码

    @using (Ajax.BeginForm("AdminSearch", "AdminMenu", new {area = "Admin"}, new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchResults"}))
{
    <input type="text" name="q" />
    <input type="submit" value="Search"/>
}

<div id="searchResults">

</div>

我的部分视图中的代码 _adminSearch

<div id="searchResults">
    <div class="entitybox">
        @{
            var grid = new WebGrid(
                Model,
                defaultSort: "Name", canPage: false
                );
        }

        @grid.GetHtml(
            tableStyle: "_tableGrid",
            columns: grid.Columns
                (
                    grid.Column("Name", "Name", item => @Html.ActionLink((string)item.Name, "SelectRecord", new { controller = "Menu", agencyKey = item.Id, name = item.Name }))
                )
           )

    </div>
</div>

控制器代码

public class AdminMenuController : Controller
    {

        public ActionResult AdminMenu()
        {
            return View();
        }

        public PartialViewResult AdminSearch(string q)
        {

            Records results = AgencyBusiness.GetAdminSearch(q);
            return PartialView("_adminSearch", results);

        }

    }

单击搜索按钮时,没有任何反应。如果您在控制器类中的 AdminSearch 方法上设置断点,它将永远不会被命中。

在此先感谢您的时间。

在此处输入图像描述

4

3 回答 3

0

I solved this issue. Odd thing. I used a shared view to show the current user at the top of the view using the following line.

@RenderPage("~/Views/Shared/_LoginBar.cshtml")

The main problem was that @Ajax.BeginForm function did not output any form tags to the browser. By using the following line instead, the form tags appeared in the HTML source and the ajax function worked.

@Html.Partial("~/Views/Shared/_LoginBar.cshtml")
于 2012-04-10T20:15:01.283 回答
0

使用 Ajax.BeginForm 时需要包含 MicrosoftAjax.js 文件。

于 2012-04-06T20:27:06.423 回答
0

我认为问题在于您的主视图和部分视图中都有一个名为“searchResults”的 div。Ajax 可能会感到困惑。

于 2012-04-06T20:21:51.010 回答