我怎样才能建立在ASP.NET MVC选项卡式菜单? [英] How can I build a tabbed menu in ASP.NET MVC?

查看:99
本文介绍了我怎样才能建立在ASP.NET MVC选项卡式菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个标签式菜单$ P $类似计算器的配置文件管理ptty。

I would like to build a tabbed menu pretty similar to the profile management of StackOverflow.

当你看看在URL,它说:/用户/ flesym标签=统计或选项卡= preFS?
我能够创建标签菜单,但我想知道我怎么能叫的操作方法,并根据选定的选项卡显示结果。

When you take a look at the url, it said: /users/flesym?tab=stats or ?tab=prefs. I was able to create the tabbed menu, but I'd like to know HOW can I call an action method and display the result depending the selected tab.

我试着使用的局部视图。但正如我的页面/用户/ flesym从Mvc.ViewPage(myApplication.Models.User)继承,我不能在我的部分观点用另一种继承(比如我想使用Mvc.ViewUserControl(myApplication.Models。格式))。

I tried using a partial view. But as my page /users/flesym inherits from Mvc.ViewPage(myApplication.Models.User), I can't use another inheritance in my partial view (for example, I'd like to use Mvc.ViewUserControl(myApplication.Models.Format)).

这是如何做到这一点有什么想法?

Any thoughts on how to do it ?

推荐答案

创建视图模型:

public class UserViewModel {
    public myApplication.Models.User User;

    public string PartialViewName;

    public PartialViewModelBase Tab;
}

对于每个选项卡,从PartialViewModelBase衍生创建视图模型:

Create View Models for each Tab, derived from PartialViewModelBase:

public abstract class PartialViewModelBase {
}

public class Tab1PartialViewModel : PartialViewModelBase {
    ...
}

public class TabNPartialViewModel : PartialViewModelBase {
    ...
}

然后,让您查看和PartialViews强类型:

Then make your View and PartialViews strongly typed:

查看:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UserViewModel>" %>

PartialViews:

PartialViews:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tab1PartialViewModel>" %>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TabNPartialViewModel>" %>

然后在你查看你可以用你的部分看法吧:

Then in your View you can use your Partial Views as:

<% 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);
}

希望这有助于。

这篇关于我怎样才能建立在ASP.NET MVC选项卡式菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆