背景:
我正在尝试实现一个选项卡控件。选项卡如下:
| Funds | Companies | Groups |
并实现如下:
<ul class="ActionControl">
<li>
<%=Html.ActionLink(string.Format("Funds ({0})", Model.Summary.FundCount)
, "ResultsList", new {selectedTab = "Funds"} ) %>
</li>
// more tabs removed for brevity, one <li> for each
</ul>
每个选项卡都是一个指向操作方法的链接,其定义为:
public PartialViewResult ResultsList(SearchViewModel search)
{
var result = new SearchViewModel
{
Summary = _entitySearchService.GetSearchDataSummary(search.SearchExpression),
PagedFunds = _fundService.GetPagedEntitiesByName<Fund>(10, search.SearchExpression),
PagedCompanies = _companyService.GetPagedEntitiesByName<Company>(10, search.SearchExpression),
PagedGroups = _groupService.GetPagedEntitiesByName<CompanyGroupInfo>(10, search.SearchExpression)
};
}
这可行,但不正确,因为我只想返回单击的选项卡的数据。我可以对 search.SelectedTab
中存储的值执行 switch()
并仅填充与单击的选项卡关联的属性,但这很混乱。
另外,这在部分视图中提出了一个问题,因为我必须区分选项卡以确定应该使用哪个选项卡填充表。目前,如果您单击“资金”选项卡,我将这样填充结果面板:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SearchViewModel>" %>
<% Html.RenderPartial("Tabs"); %>
<table>
<% foreach (var item in Model.PagedFunds) {%>
<tr>
<td><%= item.Name %></td>
</tr>
<% }%>
</table>
作为参考,这是 SearchViewModel:
public class SearchViewModel
{
public string SearchExpression { get; set; }
public string SelectedTab { get; set; }
public SearchDataSummary Summary { get; set; }
public Paging<Fund> PagedFunds { get; set; }
public Paging<Company> PagedCompanies { get; set; }
public Paging<CompanyGroupInfo> PagedGroups { get; set; }
}
分页的签名是:
public class Paging<T> : List<T> // contains functionality to manage paging and act like a List<T>
问题:
对我来说解决这个问题的最佳方法是什么?
我应该为每个选项卡设置单独的操作方法吗?因此,单击 Funds
会转到
public PartialViewResult Funds(SearchViewModel search)
并返回 ViewUserControl>
,单击 Companies
会转到
public PartialViewResult Companies(SearchViewModel search)
并返回 ViewUserControl。 Company>>
理想情况下,我可以保留单个 ResultsView
操作方法,并采用更通用的方法来返回分页项目。我认为这是可能的,但我太陷入细节之中,无法只见树木。有人可以指出我可以采取的方法吗?谢谢
Background:
I'm trying to implement a tab control. The tabs are as follows:
| Funds | Companies | Groups |
and are implemented as follows:
<ul class="ActionControl">
<li>
<%=Html.ActionLink(string.Format("Funds ({0})", Model.Summary.FundCount)
, "ResultsList", new {selectedTab = "Funds"} ) %>
</li>
// more tabs removed for brevity, one <li> for each
</ul>
Each tab is a link to an action method, which is defined as:
public PartialViewResult ResultsList(SearchViewModel search)
{
var result = new SearchViewModel
{
Summary = _entitySearchService.GetSearchDataSummary(search.SearchExpression),
PagedFunds = _fundService.GetPagedEntitiesByName<Fund>(10, search.SearchExpression),
PagedCompanies = _companyService.GetPagedEntitiesByName<Company>(10, search.SearchExpression),
PagedGroups = _groupService.GetPagedEntitiesByName<CompanyGroupInfo>(10, search.SearchExpression)
};
}
This works, but it's not correct, because I only want to bring back data for the tab that's clicked. It's possible for me to do a switch()
on the value stored in search.SelectedTab
and populate only the property that's associated with the clicked tab, but this is messy.
Also, this presents a problem in the Partial View, becuase I have to differentiate between the tabs to identify which one I should populate the table with. Currently this is how I'd populate the results panel if you clicked the Funds tab:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SearchViewModel>" %>
<% Html.RenderPartial("Tabs"); %>
<table>
<% foreach (var item in Model.PagedFunds) {%>
<tr>
<td><%= item.Name %></td>
</tr>
<% }%>
</table>
For reference, this is SearchViewModel:
public class SearchViewModel
{
public string SearchExpression { get; set; }
public string SelectedTab { get; set; }
public SearchDataSummary Summary { get; set; }
public Paging<Fund> PagedFunds { get; set; }
public Paging<Company> PagedCompanies { get; set; }
public Paging<CompanyGroupInfo> PagedGroups { get; set; }
}
and the signature for Paging is:
public class Paging<T> : List<T> // contains functionality to manage paging and act like a List<T>
Question:
What's the best way for me to approach this issue?
Should I have separate action methods for each tab? So clicking Funds
goes to
public PartialViewResult Funds(SearchViewModel search)
and returns ViewUserControl<Paging<Fund>>
and clicking Companies
goes to
public PartialViewResult Companies(SearchViewModel search)
and returns ViewUserControl<Paging<Company>>
Ideally I could keep the single ResultsView
action method and have a more generic approach to returning the paged items. I think this is possible but I'm getting too bogged down in the details and I'm not able to see the wood for the trees. Could somebody please point out an approach I could take? Thanks
发布评论
评论(1)
当我们在 MVC 站点之一中实现选项卡 (JQuery UI) 时,我们为每个选项卡采用单独的操作方法。在我们的例子中,选项卡足够不同,无论如何我们都必须为每个选项卡提供单独的部分视图。
即使您可以使用相同的partialView,我认为通过为每个选项卡使用不同的方法(考虑可读性、问题跟踪、未来的增强),您的头痛和维护也会少得多。保持单一行动,想想如果……如果公司需要自己的部分视图怎么办,如果基金需要一些不同的搜索标准怎么办,如果我们必须添加更多选项卡怎么办?当然,您可以在当前设置中解决所有这些问题,但在什么时候它会变得无法维护。恕我直言,通过不同的行动方法能够更好地解决所有这些问题以及更多问题。
When we implemented tabs (JQuery UI) in one of our MVC sites, we went with the separate action methods for each tab. In our case, the tabs were different enough that we had to have separate partialViews for each tab anyways.
Even if you can use the same partialView, I think you will have much less headache and maintenance by using distinct methods for each tab (think readability, issue tracing, future enhancements). Keeping the one action, think of the what if's....What if companies needs its own partial view, what if funds needs some different search criteria, what if we have to add more tabs? Sure, you can solve all of these in the current setup, but at what point does it become un-maintainable. Being able to solve all of these and more will be better with the distinct action methods IMHO.