ASP.NET MVC3:+的WebGrid阿贾克斯过滤器+ Ajax的排序和放大器;分页 [英] ASP.NET MVC3: WebGrid + Ajax Filters + Ajax Sorting & Paging

查看:96
本文介绍了ASP.NET MVC3:+的WebGrid阿贾克斯过滤器+ Ajax的排序和放大器;分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我使用的WebGrid,我需要过滤结果。第一个问题,我这里是它的使用的WebGrid我第一次,我希望你们中的一些可以帮我个忙吧......到目前为止,我已经成功地电网结果进行排序,并与阿贾克斯进行筛选,但是,当重新排序经过滤的结果中,该子集将丢失,我回去与完整的结果集的开始。我知道为什么它的发生,当然,但我不知道如何使它发挥作用。

Basically, I'm using WebGrid and I need to filter the results. The first problem I have here is it's my first time using WebGrid and I was hoping some of you could give me a hand with it... So far, I've managed to sort grid results and filter them with Ajax, but, when re-sorting the filtered results, the subset is lost and I go back to the beginning with the full set of results. I know why it's happening of course, but I don't figure out how to make it work.

例如:

在我的观点:

@model IQueryable<Cities>
@section MoreScripts
{
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
}

@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace,     UpdateTargetId = "GridData"}))
{
    <fieldset>
        <legend>Search Filters</legend>
        <br />
        <div>
            Name
        </div>
        <div>
            @Html.TextBox("Name")
        </div>
        <p>
            <input type="submit" value="Search" />
        </p>
    </fieldset>
}

<div id="GridData">
    @Html.Partial("Grid", Model)
</div>

我的部分观点:

@model IQueryable<Cities>

@{
    var grid = new WebGrid<Cities>(null,rowsPerPage: 5, defaultSort: "Nombre", ajaxUpdateContainerId: "GridData");
    grid.Bind(Model, autoSortAndPage: true, rowCount: Model.Count());
    @grid.GetHtml(columns: 
                    grid.Columns(
                                grid.Column("Name", "Name", canSort: true),
                                grid.Column("CreationDate", "Creation Date", canSort: true),
                                grid.Column("Active", "Active", canSort: true, format: @<text><input type="checkbox" disabled="disabled" value="@item.ID" @(item.Active == true ? "Checked" : null) /></text>),
                                                                            grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "editLink smallCell", @title = "Edit" })),
                                grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "deleteLink smallCell", @title = "Delete" }))),
                          tableStyle: "webgrid",
                          headerStyle: "webgrid-header",
                          footerStyle: "webgrid-footer",
                          alternatingRowStyle: "webgrid-alternating-row",
                          selectedRowStyle: "webgrid-selected-row",
                          rowStyle: "webgrid-row-style");    
}

最后我是什么做错了,是在这里,我的控制器:

And finally what am doing wrong is here, on my controller:

    public ActionResult Index()
    {
        return View(repository.GetAllRecords().OrderByDescending(f => f.CreationDate));
    }

    [HttpPost]
    public ActionResult Index(string name)
    {
        var data = repository.GetAllRecords();

        if(!string.IsNullOrEmpty(name))
            data = data.Where(a => a.Name.Contains(name));

        data = data.OrderByDescending(f => f.CreationDate);

        return PartialView("Grid", data);
    }

我还使用一类的WebGrid:的WebGrid因为在这里看到:
<一href=\"http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667\" rel=\"nofollow\">http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667

所以,这其实工作正常进行过滤,但一旦你得到的筛选结果,然后尝试改变你缩小搜索结果的排序顺序,你失去的元素和名称参数的值,因为的WebGrid去agains的第一个控制器的动作。也许这是不是最好的办法,但正如我所说,我从来没有用过的WebGrid所以我很愿意学习。任何帮助将是非常美联社preciated。谢谢你。

So, this actually works fine for filtering, but once you get the filtered results and then try to change the sort order of your narrowed search results, you lose the elements and the 'name' parameter's value because the WebGrid goes agains the first controller action. Probably this is not the best approach, but as I said, I've never used WebGrid so I'm willing to learn. Any help would be really appreciated. Thanks.

推荐答案

尝试FormMethod.Post添加到您的形式,因此请求将进入第二的ActionResult。否则,该请求是GET,并进入到第一的ActionResult指数(),而不的参数。

Try to add FormMethod.Post to your form, so the request will go to the second ActionResult. Otherwise, the request is a GET, and goes to the first ActionResult Index() without the parameters.

这篇关于ASP.NET MVC3:+的WebGrid阿贾克斯过滤器+ Ajax的排序和放大器;分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆