0

我有一个 Search.cshtml 视图,看起来像这样:

@model IEnumerable<MVC.Models.Books>

@using (Html.BeginForm())
{
    @Html.DropDownList("Categorie", ViewBag.Categorie as SelectList); 
    @Html.TextBox("Query");
    <input type="submit" value="Search" />
}

@if (this.Model.Any())
{
    var grid = new WebGrid(Model, rowsPerPage:20);

    @grid.GetHtml(mode: WebGridPagerModes.All,
    columns: grid.Columns(
        grid.Column(columnName: "Number"),
        grid.Column(columnName: "Title"),
        grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Nummer })),
        grid.Column(format: (item) => Html.ActionLink("Details", "Details", new { id = item.Nummer })),
        grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Nummer }))
        )
    )
}

我的控制器有以下方法:

        public ActionResult Search()
        {
            ViewBag.Categorie= new SelectList(new[] { "Number", "Title"});
            IEnumerable<Books> model = new List<Books>();
            return View(model);
        }

        [HttpPost]
        public ActionResult Search(string categorie, string query)
        {
            ViewBag.Categorie= new SelectList(new[] { "Number", "Title"});    
            IEnumerable<Books> model;

            switch (categorie)
            {
                case "Number":
                    int qnumber = Convert.ToInt32(query);
                    model = _db.Books.Where(b => b.Number == qnumber)
                                    .OrderBy(b => b.Number).ToList();
                    break;
                default:
                    model = _db.Books.Where(b => b.Title.Contains(query))
                                    .OrderBy(b => b.Title).ToList();
                    break;
            }
            return View(model);
        }

一切正常。但是列和分页链接的排序不起作用。如果单击其中一个,视图将重新加载,而网格中没有数据。

有没有办法让 WebGrid 在这个搜索视图中工作?

4

2 回答 2

0

在您的代码中:

@if (this.Model.Any())
{
   <div id="grid" class="ajaxGrid">
   @{
       var grid = new WebGrid(Model, rowsPerPage: 8, canPage: true, canSort: true, ajaxUpdateContainerId: "grid");
   }

   @grid.GetHtml(mode: WebGridPagerModes.All,
   columns: grid.Columns(
   grid.Column(columnName: "Number"),
   grid.Column(columnName: "Title"),
   grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Nummer })),
   grid.Column(format: (item) => Html.ActionLink("Details", "Details", new { id = item.Nummer })),
   grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Nummer }))))
</div>
}

- 编辑 -

    public ActionResult Fetch(string sort, string sortDir)
    {
        //sorting logic here
        return View();
    }
于 2013-02-28T10:10:25.357 回答
0

如果没有 Ajax,我就无法正常工作。所以现在我的解决方案如下所示:

“搜索.cshtml:”

<h2>Search</h2>

@using (Ajax.BeginForm("Update", "Book", new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "searchResults"
}))
{
    @Html.DropDownList("categorie", new SelectList(new[] { "Number", "Writer", 
                "Title"}) as SelectList)
    @Html.TextBox("query")
    <input type="submit" value="Search" />
}
<br /><br />

<table id="searchResults">
</table>

部分视图“_BookResults.cshtml”:

@model IEnumerable<MVC.Models.Books>

@{
    var grid = new WebGrid(Model, defaultSort: "Number", rowsPerPage: 20, ajaxUpdateContainerId: "searchResults");
    @grid.GetHtml(mode: WebGridPagerModes.All,
    htmlAttributes: new { id = "searchResults" },
    columns: grid.Columns(
        grid.Column(columnName: "Number"),
        grid.Column(columnName: "Title"),
        grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Nummer })),
        grid.Column(format: (item) => Html.ActionLink("Details", "Details", new { id = item.Nummer })),
        grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Nummer }))
    ));
}

Controller 现在有以下方法:

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

public PartialViewResult Update(string categorie, string query)
{
    var books = new List<Books>();

    switch (categorie)
    {
        case "Number":
            int qnumber = Convert.ToInt32(query);
            books = _db.Books
                .Where(b => b.Number== qnumber).ToList();
            break;
        case "Writer":
            books = _db.Books
                .Where(b => b.Writer.Name.Contains(query))
                .OrderBy(b => b.Writer.Name).ToList();
            break;
        case "Title":
            books = _db.Books
                .Where(b => b.Title.Contains(query))
                .OrderBy(b => b.Title).ToList();
            break;
    }

    return PartialView("_BookResults", books);
}

我希望这个解决方案能帮助遇到类似问题的每个人。

于 2013-04-16T11:38:16.533 回答