MVC表排序在使用路由值时工作正常,但在使用查询字符串参数时则不行

问题描述 投票:1回答:2

我有一个MVC 5应用程序(C#和BootStrap),当然有些页面上有表格。我使用了在Microsoft tutorial中发现的推荐的列排序方法,该方法似乎工作正常。它通过在ViewBag中传递列排序信息来工作。

这里是URL的示例:... localhost / PhoneDirectory / Employee / Search / chris / dept_desc该URL对chris名称进行搜索,并按Department列降序排列。效果很好。

并且借助MVC的魔力,它也可以工作(使用查询字符串参数代替URL):... localhost / PhoneDirectory / Employee / Search?query = chris&sortorder = dept_desc

但是,当我将第二个URL与querystring参数一起使用时,似乎破坏了我的列排序。例如,当我再次单击“部门”列时,URL就是这样显示的:... localhost / PhoneDirectory / Employee / Search?sortOrder = Department注意查询参数在回发后是如何消失的。

这是我为此页面配置的路由。

        routes.MapRoute(
            name: "NameSearchRoute", 
            url: "Employee/Search/{query}/{sortOrder}", 
            defaults: new { controller = "Employee", action = "Search", sortOrder = UrlParameter.Optional }
        );

我想在回发期间对排序保留查询参数。我该怎么办?

c# asp.net-mvc razor routes
2个回答
0
投票

请注意,所提供的教程和示例有点简单,在现实世界中,您的页面可能需要比它们在此处回答的更多行为和极端情况。这很好地演示了某些功能,但仅适用于一次拍摄。通常,在现实世界中,用户会像您一样继续与页面进行交互。

@using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString")  
        <input type="submit" value="Search" /></p>
}

每次加载此页面时,输入值为空。您需要一种方法来设置该值,以便当用户再次单击时,回发将具有与第一次相同的数据。

下一步是将页面“状态”保存在某处,并随每个请求来回发送。您已经使用ViewBag做了一些操作,但是通常我们为此使用模型绑定。如果您继续学习教程,则应该很快找到模型绑定。本教程确实是保存SearchTerm的。我在页面的下方(在介绍分页的部分中)看到了这一点,他们为此使用ViewBag。

Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)

因此,在Index()操作的末尾,请确保您正在设置ViewBag.CurrentFilter。


0
投票

注意:查看页面源代码以查看此处发生的情况确实有帮助。这是我必须做的...以前,我的视图如下所示(根据Microsoft教程):

<table class="table">
    <tr>
        <th class="hidden-xs">

        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.LastNameFirstName).ToHtmlString(), "Search", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th class="hidden-xs">
            @Html.DisplayNameFor(model => model.FormattedPhone)
        </th>
        <th class="hidden">
            @Html.DisplayNameFor(model => model.FormattedPhone)
        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.JobTitle).ToHtmlString(), "Search", new { sortOrder = ViewBag.TitleSortParm })
        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.DepartmentName).ToHtmlString(), "Search", new { sortOrder = ViewBag.DeptSortParm })
        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.FacilityName).ToHtmlString(), "Search", new { sortOrder = ViewBag.LocSortParm })
        </th>
    </tr>

但是当我将查询参数添加到标题中时,它的工作非常出色。

<table class="table">
    <tr>
        <th class="hidden-xs">

        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.LastNameFirstName).ToHtmlString(), "Search", new { query = ViewBag.Message, sortOrder = ViewBag.NameSortParm })
        </th>
        <th class="hidden-xs">
            @Html.DisplayNameFor(model => model.FormattedPhone)
        </th>
        <th class="hidden">
            @Html.DisplayNameFor(model => model.FormattedPhone)
        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.JobTitle).ToHtmlString(), "Search", new { query = ViewBag.Message, sortOrder = ViewBag.TitleSortParm })
        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.DepartmentName).ToHtmlString(), "Search", new { query = ViewBag.Message, sortOrder = ViewBag.DeptSortParm })
        </th>
        <th class="hidden-xs">
            @Html.ActionLink(Html.DisplayNameFor(model => model.FacilityName).ToHtmlString(), "Search", new { query = ViewBag.Message, sortOrder = ViewBag.LocSortParm })
        </th>
    </tr>

现在我可以从其他系统接受这样的URL:... localhost / PhoneDirectory / Employee / Search?query = chris,并且当用户单击列标题对结果进行排序时,它会创建一个新URL像这样的[[... localhost / PhoneDirectory / Employee / Search / chris / dept_desc,而这正是我想要的。

© www.soinside.com 2019 - 2024. All rights reserved.