asp.net mvc 4 - Dynamically add content to layout in MVC4 -
i have master layout file views rendered in. show message users in particular area of layout using following rules:
- within our maintenance period, show warning on every page after user has signed in.
- near our maintenance period, show warning on every page after user has signed in (but different content #1)
- during normal periods, show message upon signing in not subsequent pages
- it's possible may wish append additional messages area other views/controllers don't want have conscious of whether i'm overlapping maintenance warnings
i'm struggling right way this. right have like:
public class layoutcontroller : controller { [childactiononly] public ihtmlstring getmarginmessages() { loadmaintenancemessages(); var messages = this.viewbag.marginmessages.tosinglestring(); return new htmlstring(messages); } private list<string> loadmaintenancemessages() { if (withinmaintenanceperiod) { this.viewbag.marginmessages.add("foo"); } else if (nearmaintenanceperiod) { this.viewbag.marginmessages.add("bar"); } } } then in layout can have:
<div id="marginmessage">@html.action("getmarginmessages")</div> in other pages or controllers, can have:
this.viewbag.marginmessages.add("something") // or have go through helper of sorts is right way thinking this? i'm not thrilled using viewbag, don't see better way manage inter-view/controller sharing. , doing maintenance time period check on every view rendering doesn't feel quite right either.
what other options i'm missing?
for this, i'd have section in master layout view renders separate action.
(page stuff...) <div id="marginmessages> html.action("getmarginmessages", "infrastructure") </div> (more page stuff...) where have infrastructurecontroller controller handles cross-cutting concerns margin messages, notification messages , like. controller have method getmarginmessages works out whether messages need displayed, , if so, returns partial view containing messages rendered want them. if there no messages, can return emptyresult , should make sure page looks ok when div empty.
for more complex logic, create action filter derived actionfilterattribute catches request either after controller method (onactionexecuted()), or before view renders(onresultexecuting()). (in theory, shouldn't matter of use.)
from there, can use filtercontext to:
- see controller method , action user hit
- see values controller put model,
viewbag, ,tempdata - add/remove/alter values in model,
viewbag, ,tempdata
so there, should able set values tell message-rendering partial view needs do. partial view can retrieve these values , use them needs (just remember null-check of things before trying use them).
once set up, action filter can applied to:
- all methods, adding
globalfiltercollectioninapplication_start()method inglobal.asax.cs - an entire controller, adding above line
public class mycontroller : controllerin controller's file - a specific method, adding above method
and of course, @ of these places can pass values in constructor, , have logic in filter works out 1 overrides which.
Comments
Post a Comment