TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sneha K
1.2k
527
195.7k
Is it possible to retrieve and show view name dynamicallyMVC
Apr 11 2017 1:01 PM
Hi i want to show the View name dynamically. i will explain my issue detaily.
I brought the menu in layout Page dynamically. Now what i want is whatever name i click in menu list it redirect to that controller
Dynamic Menu in Layout Page Eg format
1) Accounts Master
1.1) Accounts
1.2) Accounts category
Now if i click accounts it go the accounts controller. At the same time i want to get that view name Accounts in controller. Then using session i can pass the name from to view to controller. Can any one tell me how i get the view name by selected value in menu layout page . Is it possible? In windows i saw this method .But i am struggling to do this in mvc.
My model
Menu Model
public class Menu
{
public Menu()
{
MenuItems = new List<MenuItem>();
SubMenuItems = new List<SubMenuviewmodel>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<MenuItem> MenuItems { get; set; }
public List<SubMenuviewmodel> SubMenuItems { get; set; }
}
SubMenuViewModel
public class SubMenuviewmodel
{
public string SubMenu { get; set; }
public string ActionName { get; set; }
public string MainMenuName { get; set; }
public string ControllerName { get; set; }
public string subid { get; set; }
}
MenuItem
public class MenuItem
{
public int Id { get; set; }
public int UserName { get; set; }
public string Name { get; set; }
public string ActionName { get; set; }
public string ControllerName { get; set; }
public string Url { get; set; }
public Menu ParentMenu { get; set; }
}
My Controller (Menu Controller)
public ActionResult SubMenu()
{
var menu = new Menu();
var mainmenu = db.ApplicationNames.Select(e => new
{
uid = e.UID,
mainmenuname = e.ApplicationName1
}).ToList();
var menuitemlist = new List<MenuItem>();
for (var item = 0; item < mainmenu.Count();item++ )
{
var mainmenuitems = new MenuItem { Name = mainmenu[item].mainmenuname.ToString(),Id=mainmenu[item].uid };
menuitemlist.Add(mainmenuitems);
}
menu.MenuItems = menuitemlist;
//submenu
var submenu = db.SubAplicationNames.Select(e => new
{
submenuuid = e.SubAplicationNameID,
submenuname = e.SubMenuName,
submenuparentidd = e.ApplicationID,
mainmenuname=e.ApplicationName
}).ToList();
var submenulist = new List<SubMenuviewmodel>();
for (var item = 0; item < submenu.Count();item++ )
{
var submenuitemslist = new SubMenuviewmodel { SubMenu = submenu[item].submenuname.ToString(),subid=submenu[item].submenuparentidd.ToString()
,ControllerName="Home",ActionName="Index" };
submenulist.Add(submenuitemslist);
}
menu.SubMenuItems = submenulist;
return View(menu);
}
My view
@model DynamicMenuLoading.Models.Menu
@{
Layout = null;
}
@foreach (var MainMenu in Model.MenuItems)
{
<a href="#">
<span>@MainMenu.Name</span>
</a>
<ul class="nav nav-second-level collapse">
@foreach (var SubMenu in Model.SubMenuItems.Where(x => x.subid == MainMenu.Id.ToString()))
{
<ul>
<li><a href="@Url.Action(SubMenu.ActionName, SubMenu.ControllerName)"><i class="fa fa-circle-o"></i>@SubMenu.SubMenu</a>
//tried @Session["Name"]
[email protected]
;
</li>
</ul>
}
</ul>
}
public ActionResult Index()
{
var viewname =Session["Name"];
}
Here this code is used to load the meny dyanmically in layout page. Now i want to pass the view name which i selected to controller. I tried to use session. That is get the name is session and tried to pass to controller . But it failed .Failed in the sense it give null values in controller and wrong output in view. Any one undertsand my issue and help me to resolve this problem.
Advance Thanks.
Reply
Answers (
1
)
a gridview row databound value change
how to set min 10 number for mobile number validation?