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 8790721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:43:46+00:00 2026-06-13T22:43:46+00:00

I am beginning to use ASP.NET MVC and i have a navigation which is

  • 0

I am beginning to use ASP.NET MVC and i have a navigation which is build with javascript and css. The Navigation is collapsable, so that the menu only takes like 1cm when its collapsed.
My problem is that i need to change the state (collapsed/expanded) by clicking on a button and setting a css class on the menu.
But when i click on a menu-entry the page gets reloaded and so the css-class gets lost too.

How can i persist such data correctly? In normal views i could pass it to my viewmodel, but how can i do this in my _layout.cshtml? (i cant set a model there, right?)

What i tried was using the jquery plugin “cookies” but it only works sometimes (i think the problem is somewhere within the options of the plugin – maybe i have to set a domain or something…). Furthermore it would be nice if the solution wouldnt require the user to allow cookies.

Im glad for any help!

edit:
ok, what i tried now is:

//the view

@model NHibernateSimpleDemoMVC.Models.MenuModel
....
<a id="togglemenu" />

@Html.HiddenFor(x => x.IsCollapsed, new { @id = "collapsedHdn" })

...

<script>
jQuery('#togglemenu').click(
    function () {
        if (jQuery("#collapsedHdn").val() == "true") {
            jQuery("#collapsedHdn").val("false")
        } else {
            jQuery("#collapsedHdn").val("true")
        }
    });

jQuery('#nav > li').click(
    function () {
        var name = jQuery(this).attr("id");
        jQuery("#selectedHdn").val(name);
    }
    );


jQuery(document).ready(function () {
    //set selected node
    if (jQuery("#collapsedHdn").val() == "true") {
       ... collapse ...
    }
     if (jQuery("#selectedHdn").val() != "") { //this is always empty ("")
        var name = "#" + jQuery("#selectedHdn").val();
        jQuery(name).addClass("current");
    }
});

</script>

//the model

public class MenuModel
{
    public bool IsCollapsed { get; set; }
    public string SelectedEntry { get; set; }

    public MenuModel(){
        IsCollapsed = false;
        SelectedEntry = "";
    }
}

… my hidden fields seem to not keep their value after the postback…

  • 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-13T22:43:48+00:00Added an answer on June 13, 2026 at 10:43 pm

    There’s a useful related question: ASP.NET MVC Razor pass model to layout

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

Sidebar

Related Questions

Since beginning to use VB.NET some years ago I have become slowly familiar with
I have an asp.net (mvc) web site. As the part of the functions I
Intro: I'm trying out LESS in an asp.net mvc environment. I use dotless for
I'm beginning to design the infrastructure of web application developed with ASP.NET MVC preview
I have an ASP.NET website that I built that has grown considerably in size
Is there a way to force use of a named route in ASP.NET MVC
Ever since I've started asp.net mvc development my experience is almost 80% jQuery, only
I have a devexpress gridview within my asp.net page which has a filter row.
I'm beginning python and I'm trying to use a two-dimensional list, that I initially
I have a button I want to use in the beginning and the end

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.