Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:48:31+00:00 2026-06-07T22:48:31+00:00

I was following a tutorial in a book on creating a categories side menu

  • 0

I was following a tutorial in a book on creating a categories side menu using a partial view.
It went fine until it got to the feature of showing the currently selected category.

The problem arises as there doesn’t seem to be any logic in passing the currently selected category to the partial view.

The layout CSHTML file has for displaying categories

@{ 
    Html.RenderAction("Menu", "Nav"); 
  }

as you can see the selected category ID is not being passed

if you look at the NavController the menu method accepts a select category ID

  public PartialViewResult Menu(int selCatID = -1)
    {
        ViewBag.SelectedCatID = selCatID;
        return PartialView(repository.Categories);

    }

But the call from the layout.html file seems to be calling the Menu without a parameter causing the selCatID to always be -1

Here is the view for Menu

 @foreach (var cat in Model)
{
 @Html.RouteLink( cat.CategoryName , new {
controller = "Company", action="List"
       , catID=cat.ID, page= 1},
    new { @class = cat.ID == ViewBag.SelectedCatID ? "selected" : null }
)
}

There doesn’t seem to be any visible code that shows the selected CatID from the main controller (Company) being passed into the view for NavController.

I figured that the main controller should be setting the viewbag for the selected ID.I did the following changed, and it worked but I am not sure if it is the correct technique. I was wondering if I am doing it correctly or have I missed out something?.

in the layout file pass a parameter in:

Html.RenderAction("Menu", "Nav", new { selCatID = ViewBag.SelCatID });

in the main entity controller (CompanyController) set the viewbag of the current selected ID

public class CompanyController : Controller
{
    .....
 public ViewResult List(int catID = -1, int page = 1)
      {
       CompaniesListViewModel viewModel = new CompaniesListViewModel
        {
         ...........
        };

        ViewBag.SelectedCatID = catID;
        return View(viewModel);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:48:33+00:00Added an answer on June 7, 2026 at 10:48 pm

    The problems was this code

      Html.RenderAction("Menu", "Nav", new { selCatID = ViewBag.SelCatID });
    

    I had added the parameter in as without it was passing in a null value and crashing it on the first run. So I changed it back to:
    Html.RenderAction(“Menu”, “Nav”);

    To handle the null, I changed the parameter to a nullable int

        public PartialViewResult Menu(int? selCatID = -1)
        {
            ViewBag.SelectedCatID = selCatID;
    

    And now all is working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following a very basic book tutorial on Mac development. We're just creating a
I'm currently following a tutorial in a book, and it instructs to create a
I am following a tutorial in the book Machine Learning for Hackers by Drew
I'm new to cakephp and following this tutorial. http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html I have created the blog
I'm following a OpenCV book's tutorial and the following code doesn't work: #include <iostream>
I am following the following tutorial ( http://www.highoncoding.com/Articles/642_Creating_a_Stock_Widget_in_ASP_NET_MVC_Application.aspx ) on using ajax to render
I'm using the following tutorial http://developer.android.com/resources/tutorials/views/hello-mapview.html in order to create a map view and
I have created a listview using following tutorial link http://www.ezzylearning.com/tutorial.aspx?tid=1763429 Outcome of this is,
I've followed the tutorial here: http://book.cakephp.org/view/1286/Sending-a-basic-message , which I have successfully used before with
I'm currently following the Shovell tutorial in the Simply Rails 2 book. On page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.