jquery - running code that replaces placeholder text on each razor view MVC4 -


if had following razor pages: (excuse terrible examples..)

page1.cshtml

hello @model.name, welcome {sitename} 

page2.cshtml

{sitename} has had @model.visitorcount today 

and @ runtime wanted replace {sitename} "contoso" (this var comes settings class) additional tags

i use jquery like:

"$(body).replace("{sitename}", "contoso") 

this happen in _layout or viewstart file cut down on script, don't approach. seems cause lot of code clutter , doesnt "seem right"

is there better approach take here? perhaps using controller base class , somehow parsing each view?

use viewbag in conjunction global filter.

e.g.

page1.cshtml

hello @model.name, welcome @viewbag.sitename 

page2.cshtml

@viewbag.sitename has had @model.visitorcount today 

sitenameintoviewbagattribute.cs

public class sitenameintoviewbagattribute : actionfilterattribute {     public override void onactionexecuted(actionexecutedcontext filtercontext)     {         filtercontext.controller.viewbag.sitename = getsitetitle();     } } 

then register in global.asax

public partial class mvcapplication : system.web.httpapplication {     protected void application_start()     {         //blah blah blah          globalfilters.filters.add(new sitenameintoviewbagattribute ());          //blah blah blah     } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -