I’m writing an MVC 3 app and I have tried to code a control in the Controller (due to permissions, different menu items will be visible for different users) and use the object in the Razor page. For example, in the Controller I do something like:
public ActionResult Index()
{
var menu = "@(Html.Telerik().Menu().Name("menu").Items("menus => { menus.Add().Text("Home").Action("Index", "Home"); menus.Add().Text("Deliveries").Action("Index", "Delivery"); }))";
var model = new MenuModel()
{
Menu = menu
};
return View(model);
}
And in the View I try to render the Menu using @Model.Menu but I just get the string value rather than an actual menu. Is what I’m trying to do possible?
Extend the
HtmlHelperclass and use the newly created method to render your menu in the View:Helper:
View:
Nevertheless, it is fine to put this logic in the view. Using the
HtmlHelperextension just separates/cleans the code.