asp.net Mvc 4中的通用面包屑 [英] Generic breadcrumbs in asp.net Mvc 4

查看:121
本文介绍了asp.net Mvc 4中的通用面包屑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



请帮助我,因为我是asp.net mvc的新手。我想在asp.net mvc中实现通用面包屑。所以我创建了一个局部视图(cshtml)以及所有页面和部分视图中的所有内容,我嵌入逻辑来显示BreadCrumbs及其工作正常。但问题是,页面可以以两种方式打开



1)通过仪表板

2)通过菜单



我的编码只处理一个场景(仪表板或菜单),任何人都可以简化这个场景或指导我在所有页面中应用痕迹。



所以我想要这个



1)如果用户从菜单进入客户页面,则面包屑应显示菜单中的层次结构,如Admin>客户



2)如果用户从仪表板进入客户页面,则面包屑应显示仪表板中的层次结构,如仪表板>客户



先谢谢。



问候。

Dear All,

Please help me as i m totally new to asp.net mvc.I want to implement generic breadcrumb in asp.net mvc. So i created a partial view(cshtml) and all it in all pages and in the Partial View,i embed the logic to show BreadCrumbs and its working fine.But problem is that, a page can be open in two ways

1)through dashboard
2)through Menu

My Coding handles only one scenario(either dashboard or menu), Can anyone please simplify this scenario or guide me to apply breadcrumb in all the pages.

So i want this

1)if a user enters Customer's page , from the menu then the breadcrumb should show the hierarchy from the menu like Admin > Customer

2)and if a user enters customer's page from the dashboard then the breadcrumb should show the hierarchy from the dashboard like Dashboard > Customer

Thanks in Advance.

Regards.

推荐答案

让我们考虑场景,因为我们将使用MVC以及Razor,我们可以绑定来自Controller的视图上的数据。

该控制器将获取此类数据以进行绑定作为视图上的面包scrum。

由于有两种导航方式,你必须专注于动作和控制器。

因为这是找出的唯一方法当前控制器以及将呈现当前页面的当前操作。

此控制器名称以及操作名称将出现在URL中。

借助于这一对,在数据库中创建表,并为Bread-scrum添加条目。

通过在参数中传递两个参数,即控制器名称和操作名称,进行通用的Ajax调用以从数据库获取此数据将导致Bread-scrum名称。

将此Ajax调用的结果绑定到部分v IEW。

这使得通用的Bread-scrum。
Lets consider scenario as we are going to use MVC as well as Razor, we can bind the data on the view coming from the Controller.
That controller will fetch such data to bind as Bread-scrum on the view.
As there are two ways of Navigation, you have to concentrate on Actions and Controller.
As this is the only way to find out the current controller as well as current action which will render the current page.
This controller name as well as Action name will be present in the URL.
With the help of this pair, make table in the Database, and add entries for the Bread-scrum.
Make a generic Ajax call to get this data from the Database by passing two in Parameters i.e. Controller Name and Action Name which will results in Bread-scrum name.
Bind this Ajax call's result to the partial view.
This makes generic Bread-scrum.


您是否看过开源解决方案 MvcBreadCrumbs ?使用这个Nuget包有几种方法可以实现它。您可以在控制器方法上使用如下属性:



Have you seen the open-source solution MvcBreadCrumbs? There's a couple of ways to accomplish it with this Nuget package. You can use attributes on controller methods like this:

[BreadCrumb(Label="Widget")]
public ActionResult ...





或者可选择添加一个静态方法调用来覆盖标签,如果它依赖于数据:





or optionally add a static method call to override the label if it's data dependent:

[BreadCrumb(Label="Widget")]
public ActionResult Get(int id)
{
    [make db call]
    BreadCrumb.SetLabel(db.ItemDescription);
    return view(dbModel);
}





祝你好运!



Good Luck!


这篇关于asp.net Mvc 4中的通用面包屑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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