渲染查看HTML code这是在asp.nt MVC的控制器产生 [英] Rendering html code in view which is generated in controller of asp.nt mvc
问题描述
我实现基于角色的菜单。我必须动态生成从数据库菜单。我从哪里可以得到映射到该角色的功能角色的特征映射表。一旦我得到了我要生成HTML菜单以< UL>和<立GT;
。我能得到的建议是到要如何做。我的意思是在生成控制器中的HTML脚本,并使其在各自的观点。请帮忙。任何建议表示欢迎。
I am implementing a role based menu. I have to generate the menu from the database dynamically. I have a role-feature mapping table from where I can get the features that are mapped to the role. Once I get that I have to generate the HTML menus with <ul> and <li>
. Can I get suggestion as to how that be done. I mean generating the HTML script in controller and rendering it on to the respective view. Please help. Any suggestion is welcomed.
<ul id="menu">
<li>
@Html.ActionLink("Home", "Dashboard", "User")
</li>
<li>
<a href="#"><span>User</span></a>
<ul>
<li>@Html.ActionLink("Create User", "CreateUser", "User")</li>
</ul>
</li>
<li>
<a href="#"><span>Report</span></a>
<ul>
<li>@Html.ActionLink("ABC Report", "ABC", "Report")</li>
<li>@Html.ActionLink("User Report", "UserReport", "Report")</li>
</ul>
</li>
<li>
<a href="#"><span>XYZ</span></a>
<ul>
<li>@Html.ActionLink("XYZ1", "XYZ1", "XYZ")</li>
<li>@Html.ActionLink("XYZ2", "XYZ2", "XYZ")</li>
<li>@Html.ActionLink("XYZ3", "XYZ3", "XYZ")</li>
<li>@Html.ActionLink("XYZ4", "XYZ4", "XYZ")</li>
</ul>
</li>
</ul>
以上HTML我必须建立在控制器和渲染视图。
Above HTML I have to build in controller and render to the view.
推荐答案
我建议创建一个菜单
类,如下
I suggest to create a Menu
class as below
public class Menu
{
public string Text {get;set;}
public string Controller {get;set;}
public string Action {get;set;}
}
然后在视图模型
添加类型的属性菜单列表
列表&LT;菜单&gt;
然后填写该集合在你的控制器动态地从你的数据库
then in your viewModel
add a property MenuList
of type List<Menu>
then fill that collection in your controller dynamically from your database
然后更改您的视图如下
<a href="#"><span>XYZ</span></a>
<ul>
@for(var menu in Model.MenuList)
{
<li>@Html.ActionLink(menu.Text, menu.Action, menu.Controller)</li>
}
</ul>
这篇关于渲染查看HTML code这是在asp.nt MVC的控制器产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!