使用部分视图
我正在尝试创建一个包含网格和搜索的页面。问题是我想要一个网格的部分视图和一个用于搜索的视图。
如果进行搜索,这应该使用新信息呈现网格部分视图。
目前,我需要来自网格(当前存储在视图数据中)的信息,例如我要排序的列等,以便进行搜索,因为我想保留这些设置。不过,此信息仅在网格部分中可用。
最好的方法是什么,可以使其代码整洁、美观,但又不会造成混乱?
我可以在哪里存储其他部分视图中所需的信息?
局部视图1;
<table>
<%= Html.CreateGrid(Model, "Grid", "Grid", (int)ViewData["SortColumn"], (bool)ViewData["SortedASC"])%>
</table>
局部视图2;
<div class="searchControl">
<input type="text" class="SearchBox" href="<%= Url.Action("Grid", "Grid", new {page = 1, columnToSortBy=/* would like to access viewdata from partial view 1 here. */, sortASC = /* would like to access viewdata from partial view 1 here. */ } ) %>" />
<input type="submit" value="Search" class="SearchButton" />
</div>
我知道我可能会采取完全错误的方法,所以请随时指出我正确的方法!
谢谢!
I'm trying to create a page that contains a grid and searching. The issue is that I want to have a partial view for the grid and one for the searching.
If doing a search, this should render the grid partial view with the new information.
At the moment I need information, such as what column I'm sorting by and so on, from the grid (currently stored in viewdata), in order to do the search as I want to keep those settings. This information is only available in the grid partial though.
What's the best approach of this to make it neat and nice in the code, but not a mess to work with?
Where can I store information that I need in the other partial view?
Partial View 1;
<table>
<%= Html.CreateGrid(Model, "Grid", "Grid", (int)ViewData["SortColumn"], (bool)ViewData["SortedASC"])%>
</table>
Partial View 2;
<div class="searchControl">
<input type="text" class="SearchBox" href="<%= Url.Action("Grid", "Grid", new {page = 1, columnToSortBy=/* would like to access viewdata from partial view 1 here. */, sortASC = /* would like to access viewdata from partial view 1 here. */ } ) %>" />
<input type="submit" value="Search" class="SearchButton" />
</div>
I know I might take the completely wrong approach on this, so feel free to point me in the right one!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewData 是存储在视图和局部视图中访问的数据的好地方。
如果您使用强类型视图就更好了。然后,您可以访问数据以通过类型化模型对过滤进行排序。
我会让模型类实现一个接口 IGridFeatures,该接口具有 SortedASC、SortColumn、Page 的属性。
将这些可选属性放在查询字符串中而不是在路由中通常是个好主意。
ViewData is a good place to store data that is accessed in Views and Partials.
Even better if you use strongly typed views. Then you could access the data for sorting an filtering via a typed model.
I would have the model-classes implement an interface IGridFeatures that has properties for SortedASC, SortColumn, Page.
Its often a good idea to have these optional properties not in the route but in a querystring.
我认为你会更好地通过 javascript 控制你的链接,因为你真正想要的只是控制 UI。
I think you'll be better of controlling your link through javascript, since all you really want is to control the UI.