创建视图模型:
public class UserViewModel {
public myApplication.Models.User User;
public string PartialViewName;
public PartialViewModelBase Tab;
}
为每个选项卡创建视图模型,派生自 PartialViewModelBase:
public abstract class PartialViewModelBase {
}
public class Tab1PartialViewModel : PartialViewModelBase {
...
}
public class TabNPartialViewModel : PartialViewModelBase {
...
}
然后使您的 View 和 PartialViews 强类型化:
View:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UserViewModel>" %>
部分视图:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tab1PartialViewModel>" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TabNPartialViewModel>" %>
然后在您的视图中,您可以将部分视图用作:
<% Html.RenderPartial(Model.PartialViewName, Model.Tab); %>
在您的控制器操作中:
public ActionResult YourAction(string tab)
{
// check if tab is valid !!!
var model = new UserViewModel {
User = new myApplication.Models.User();
PartialViewName = tab;
Tab = TabRepository.GetTabByName(tab);
/*
* or
* Tabs = (new Dictionary<string, type> {
* {"Tab1", typeof(Tab1PartialViewName)},
* {"TabN", typeof(TabNPartialViewName)}
* })[tab];
*/
};
Return View(model);
}
希望这可以帮助。