Monday, 15 February 2010

asp.net mvc - MVC _Layout with Action using Session -order of loading is an issue for me -



asp.net mvc - MVC _Layout with Action using Session -order of loading is an issue for me -

all,

i have mvc _layout.cshtml calls:

@html.action("getactionstrip", "vehicles")

i have controller loads views.

my issue have controller action called getvehicledetails gets vehicle id.

the action on:

@html.action("getactionstrip", "vehicles")

requires getvehicledetails loads first need set vehicle id session.

this not working as:

@html.action("getactionstrip", "vehicles")

loads before getvehicledetails.

@html.action("getactionstrip", "vehicles")

needs on multiple views, that's why set in _layout file.

i can work putting:

@html.action("getactionstrip", "vehicles")

on every view need on , load in right order. ie.. controller action getvehicledetails sets vehicle id session , then:

@html.action("getactionstrip", "vehicles")

reads session value.

has got thought if can way want or have set @html.action on every view kind of breaks dry principle.

thanks russ

paul, tried write comment character limit killed me. here comment:

paul,

thanks message. understand have written before go on , seek implement want sure understand, fully, scenario.

will scenario cater fact code needs session value called @html.action in _layout (master page speak) whereas need controller action receive parameter , set parameter session.

from understand, _layouts (master pages) load first @html.action run , session value. but, code in _layout, run first , hence on controller has not yet set session actions passed in parameter.

note: _layout doesnt have it's own controller (not sure if matters)

thanks russ

something design fundamentally incorrect. shouldn't have different components beingness tightly coupled this. order shouldn't matter 1 comes first.

here how might you're looking for. i'd create model bound class can receive in controller actions need session value. model pull session value database or wherever if hasn't been set yet otherwise uses session value. order doesn't matter. improve yet create mysessionobject interface , can mock out in test cases.

public interface imysessionobject { int getvaluex(); } public class mysessionobject : imodelbinder, imysessionobject { private httpcontextbase _httpcontext; private mysessionobject(httpcontextbase httpcontext) { _httpcontext = httpcontext; } public int getvaluex() { if (_httpcontext.session["x"] == null) { _httpcontext.session["x"] = 54; // value here. } homecoming (int)_httpcontext.session["x"]; } public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext) { var context = controllercontext.httpcontext; var obj = new mysessionobject(context); homecoming obj; } } public class homecontroller : controller { public actionresult index(imysessionobject obj) { viewbag.x = obj.getvaluex(); viewbag.message = "modify template jump-start asp.net mvc application."; homecoming view(); } public actionresult about(imysessionobject obj) { viewbag.message = "your app description page."; homecoming view(); } public actionresult contact(imysessionobject obj) { viewbag.message = "your contact page."; homecoming view(); } }

asp.net-mvc session

No comments:

Post a Comment