Amir Nahravan
Amir Nahravan

Reputation: 69

Update partial view in controller

I'm working on a new project with asp.net MVC5. In this project, I have unlimited categories with unlimited subcategories.

I want to convert my categories + categories children to menus. I know that I can create these menus on my base controller but I don't like to render categories always.

I decided to create a partial view like _MenuPartial.cshtml and generate menus when the user creates a new category or update and for the latest step, I render this partial view in my layout.

I couldn't find any resource for this situation.

Can I convert a string to partial view in controller? Or do I have to create a txt file and save my menu there and convert it to HTML each time?

Upvotes: 0

Views: 1203

Answers (1)

Nima Boobard
Nima Boobard

Reputation: 509

For this situation, I suggest you to create an action for rendering the menu. Then you can use it every where that you want to be rendered. For example in your Home.cshml

Your action controller should be like this:

public PartialViewResult MainMenu()
{
   List<YourApp.Models.Menu> menuItems = new List<YourApp.Models.Menu>();

   /* Add your menus item here */

   menuItems.Add(
        new YourApp.Models.Menu() {
            Title = "Level 1",
            SubItems = new List<YourApp.Models.Menu>() {
                new YourApp.Models.Menu() {
                    Title = "Level 2",
                    SubItems = new List<YourApp.Models.Menu>() {
                        new YourApp.Models.Menu() {
                            Title = "Level 3",
                            Url = Url.Action("SampleAction", "SampleController")
                        }
                    }
                }
            }
        }
   );
   return PartialView("~/Views/Shared/MainMenu", menuItems);
}

And here is an example of MainMenu.cshtml view for making a menu using bootstrap framework:

@model YourApp.Models.Menu;

<div class="navbar-collapse collapse navbar-right">
    <ul class="nav navbar-nav" style="font-weight:bold;">
        @* Create a for loop on first level of menu model *@
        @foreach(var firstLevel in Model){
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">@(firstLevel.Title)
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    @* Iterate on children of first level items *@
                    @foreach(var secondLevel in firstLevel.SubItems){
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">@(secondLevel.Title)
                                <b class="caret"></b>
                            </a>
                            <ul class="dropdown-menu">
                                @foreach(var thirdLevel in secondLevel.SubItems){
                                    <li><a href="@(thirdLevel.Url)">@(thirdLevel.Title)</a></li>
                                }
                            </ul>
                        </li>
                    }
                </ul>
            </li>
        }
    </ul>
</div>

You can call this code and render MainMenu.cshtml in your view (e.g. Home.cshtml or Index.cshtml):

@Html.Action("MainMenu", "Home")

Upvotes: 1

Related Questions