0

I have an MVC 4 site that renders a left sidebar with a bunch of menu links on it. When the user signs in, they are directed to a page where they search for someone. Once they've selected someone and submitted the form, an id cookie gets set.

This id determines whether or not various links in the sidebar menu are enabled, as it is a required parameter for many of the actions that those links lead to. The menu itself, since it is on every page of the site, is set up in the _Layout partial:

<div id="contentwrapper">
    <div id="left-menu">
        @Html.Action("Display", "Menu", new { Area = "" })
    </div>
    <div id="mainbody">
        <section>
            @RenderBody()
        </section>
    </div>
    <div id="footer">
    </div>
</div>

Display returns a partial view representing the sidebar menu. Since I'm rendering the menu sidebar in the _Layout, I don't have a model to work with. Is there any way I can get the parameter to my menu partial view without using a cookie?

AJ.
  • 16,368
  • 20
  • 95
  • 150
  • @Scott Selby - How? This menu is rendered on every view in the site (via the _Layout). Where would I set my ViewBag("id") value? Every public GET action? – AJ. Jan 10 '13 at 21:02
  • 1
    Your id parameter should came from somewhere. If not from a cookie then from where? Where can you calculate this id? – nemesv Jan 10 '13 at 21:04
  • @nemesv - The id parameter comes from the database after the user does a search and selects it. – AJ. Jan 10 '13 at 21:05
  • @nemesv - and yes, that is my question. – AJ. Jan 10 '13 at 21:22

1 Answers1

1

I would use a hidden field and viewbag

Public ActionResult Index(){
ViewBag.id = // set your id initially 

}

if javascript/jquery is ok with you...

$(function(){
var myID = @Html.Raw(Json.Encode(ViewBag.id));
$('#hidID').val(myID);
});

HTML

<input type="hidden" name="hidID" id="hidID" />

Then.. Display Action

Public ActionResult Display(int hidID){    
// this will be current id,
// if id is reset , pass the new one to viewbag , jquery will reset hidden field on client
}
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • Would you put the hidden field and the javascript in the _Layout partial view? – AJ. Jan 10 '13 at 21:10
  • I was under the impression that putting javascript in partial views was bad practice. http://stackoverflow.com/a/6901351/27457 – AJ. Jan 10 '13 at 21:28
  • that is not what that question was refering to, they meant don't add a link to jquery file in partial view , all references to css and js files should be in the head in the layout , you can put js code anywhere you want – Scott Selby Jan 10 '13 at 21:43