asp.net - Putting a model on every view -


i'm trying pass usermodel every view, can show on top current user information.

this baseviewmodel:

public class baseviewmodel {     public usermodel currentuser { get; set; } } 

basecontroller:

public class basecontroller : controller {     protected override void onactionexecuted(actionexecutedcontext filtercontext)     {         userrepository userrep = new userrepository();         baseviewmodel model = new baseviewmodel         {             currentuser = userrep.getcurrentuser()         };         base.onactionexecuted(filtercontext);     } } 

homecontroller: (default)

public class homecontroller : basecontroller {     public actionresult index()     {         listrepository listrep = new listrepository();         return view(listrep.getalllists());//returns ienumerable<listmodel>     } } 

my shared _layout.cshtml:

<!doctype html> <html> <head>     <meta charset="utf-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>@viewbag.title - asp.net application</title>     @styles.render("~/public/css") </head> <body>         <header>             @html.partial("_header")         </header>     @renderbody() </body> </html> 

and finally, _header.cshtml:

<div id="logo">     <h1>hello @model.currentuser.username</ h1 > </div> 

but error message when run it:

'system.collections.generic.list' not contain definition 'currentuser'

i thought baseviewmodel appended

the model in view ienumerable<listmodel> , passed partial unless specify model, example, using

@html.partial("_header", someothermodel) 

instead, create childactiononly method returns user data _header.cshtml partial view , in main view use @html.action() render it

[childactiononly] public actionresult _header() {    var model = .... // model want render in partial    return partialview(model); } 

and in main view

@{ html.renderaction("_header", controllername); } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -