C# MVC表排序在使用路由值时工作良好,但在使用querystring参数时则不行
我有一个MVC5应用程序(C#和BootStrap),当然有些页面上有表格。我使用了我在a中找到的推荐的列排序方法,它似乎工作得很好。它通过在ViewBag中传递列排序信息来工作 下面是URL的一个示例:…localhost/PhoneDirectory/Employee/Search/chris/dept_desc该URL执行名称chris的搜索,并按Department列降序排序。效果很好 在MVC的魔力下,这也能起作用(一个使用querystring参数的URL):…localhost/PhoneDirectory/Employee/Search?query=chris&sortorder=dept\u desc 但是,当我使用带有querystring参数的第二个URL时,它似乎破坏了我的列排序。例如,当我再次单击Department列时,URL是这样显示的:…localhost/PhoneDirectory/Employee/Search?sortOrder=Department注意查询参数在回发邮件时是如何消失的 以下是我为此页面配置的路由C# MVC表排序在使用路由值时工作良好,但在使用querystring参数时则不行,c#,asp.net-mvc,razor,routes,C#,Asp.net Mvc,Razor,Routes,我有一个MVC5应用程序(C#和BootStrap),当然有些页面上有表格。我使用了我在a中找到的推荐的列排序方法,它似乎工作得很好。它通过在ViewBag中传递列排序信息来工作 下面是URL的一个示例:…localhost/PhoneDirectory/Employee/Search/chris/dept_desc该URL执行名称chris的搜索,并按Department列降序排序。效果很好 在MVC的魔力下,这也能起作用(一个使用querystring参数的URL):…localhost/P
routes.MapRoute(
name: "NameSearchRoute",
url: "Employee/Search/{query}/{sortOrder}",
defaults: new { controller = "Employee", action = "Search", sortOrder = UrlParameter.Optional }
);
我想在排序回发期间维护查询参数。如何做到这一点?请注意,所提供的教程和示例有点过于简单,在现实世界中,您的页面可能需要考虑比此处更多的行为和边缘情况。这很好地演示了一些功能,但只适用于一次拍摄。通常在现实世界中,用户会像您一样继续与页面交互
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" /></p>
}
因此,在Index()操作结束时,请确保您正在设置ViewBag.CurrentFilter。请注意,提供的教程和示例有点过于简单,在现实世界中,您的页面可能需要考虑比此处更多的行为和边缘情况。这很好地演示了一些功能,但只适用于一次拍摄。通常在现实世界中,用户会像您一样继续与页面交互
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" /></p>
}
因此,在Index()操作结束时,请确保设置了ViewBag.CurrentFilter。注意:查看页面源代码以查看此处发生的情况确实很有帮助。这就是我必须做的。。。以前我的视图是这样的(根据Microsoft教程):
@ActionLink(Html.DisplayNameFor(model=>model.LastNameFirstName).ToHtmlString(),“Search”,新的{sortOrder=ViewBag.NameSortParm})
@DisplayNameFor(model=>model.FormattedPhone)
@DisplayNameFor(model=>model.FormattedPhone)
@ActionLink(Html.DisplayNameFor(model=>model.JobTitle).ToHtmlString(),“搜索”,新的{sortOrder=ViewBag.TitleSortParm})
@ActionLink(Html.DisplayNameFor(model=>model.DepartmentName).ToHtmlString(),“Search”,新的{sortOrder=ViewBag.DeptSortParm})
@ActionLink(Html.DisplayNameFor(model=>model.FacilityName).ToHtmlString(),“搜索”,新的{sortOrder=ViewBag.LocSortParm})
但是,当我将查询参数添加到标题时,它工作得非常好
<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>
@ActionLink(Html.DisplayNameFor(model=>model.LastNameFirstName).ToHtmlString(),“搜索”,新的{query=ViewBag.Message,sortOrder=ViewBag.NameSortParm})
@DisplayNameFor(model=>model.FormattedPhone)
@DisplayNameFor(model=>model.FormattedPhone)
@ActionLink(Html.DisplayNameFor(model=>model.JobTitle).ToHtmlString(),“搜索”,新的{query=ViewBag.Message,sortOrder=ViewBag.TitleSortParm})
@ActionLink(Html.DisplayNameFor(model=>model.DepartmentName).ToHtmlString(),“Search”,新的{query=ViewBag.Message,sortOrder=ViewBag.DeptSortParm})
@ActionLink(Html.DisplayNameFor(model=>model.FacilityName).ToHtmlString(),“搜索”,新的{query=ViewBag.Message,sortOrder=ViewBag.LocSortParm})
现在我可以从其他系统接受这样的URL:…localhost/PhoneDirectory/Employee/Search?query=chris,当用户单击列标题对结果进行排序时,它会创建一个新的URL,如下所示…localhost/PhoneDirectory/Employee/Search/chris/detu desc,这正是我想要的。注意:查看页面源代码以查看此处发生的情况。这就是我必须做的。。。以前我的视图是这样的(根据Microsoft教程):
@ActionLink(Html.DisplayNameFor(model=>model.LastNameFirstName).ToHtmlString(),“Search”,新的{sortOrder=ViewBag.NameSortParm})
@DisplayNameFor(model=>model.FormattedPhone)
@DisplayNameFor(model=>model.FormattedPhone)
@ActionLink(Html.DisplayNameFor(model=>model.JobTitle).ToHtmlString(),“搜索”,新的{sortOrder=ViewBag.TitleSortParm})
@ActionLink(Html.DisplayNameFor(model=>model.DepartmentName).ToHtmlString(),“Search”,新的{sortOrder=ViewBag.DeptSortParm})
@ActionLink(Html.DisplayNameFor(model=>model.FacilityName).ToHtmlString(),“搜索”,新的{sortOrder=ViewBag.LocSortParm})
但是,当我将查询参数添加到标题时,它工作得非常好
<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>
@ActionLink(Html.DisplayNameFor(model=>model.LastNameFirstName).ToHtmlString(),“搜索”,新的{query=ViewBag.Message,sortOrder=ViewBag.NameSortParm})
@DisplayNameFor(model=>model.FormattedPhone)