asp.net mvc - Derive Model from Another Model -


in asp.net mvc 3 project, i'd have base class model object has properties use in of other models in application (or @ least of them). way, views use these derived models have base class model properties.

the base class model properties set in, base class controller, , somehow need populate derived models same data. suggestions on how best 'er done?

or should looking creating own defaultmodelbinder somehow? i'm little confused options here. if couldn't tell. :)

sample code

base model

public class elkmodel {     public bool isadministrator { get; set; } } 

derived model

public class usermodel : elkmodel {     [hiddeninput]     public int user_id { get; set; }      // other properties specific model } 

so in base controller

private basemodel basemodel;  protected override void onactionexecuting(actionexecutingcontext filtercontext) {     basemodel = new basemodel();     basemodel.myproperty = "test";      base.onactionexecuting(filtercontext); } 

and in derived controller action method

public actionresult index() {     // derives base model in base controller     usermodel model = new usermodel();      // model.isadministrator isadministrator property of base model not usermodel      return view(model); } 

if need 'common data' across views, not throw inheritance in mix. go extension method.

something along lines of:

public static commondata getcommondata(this htmlhelper htmlhelper)         {             // magic here         } 

in view call:

@html.getcommondata()... 

that way keep models cleaner.

inheritance might valid approach, if common data 'fits' model classes.

it if provided examples ;-)

** edit **

i keep 'basemodel' property in controller , populate on onactionexecuting suggest.

but use extensionmethods on controller , html helper access the data. if use inheritance needlessly complicate things.

let me know if need samples of extension methods.

this approach dry. not duplicating code.

** edit 2 **

here how extension method should like: (off top of head, might not compile)

public static class extensionhelpers {     public static basemodel getcommondata(this htmlhelper htmlhelper)     {         yourcontroller htmlhelper = htmlhelper.viewcontext.controller yourcontroller;         if (controller != null)         {             return htmlhelper.basemodel;         }         return null; } 

make sure controller derives controller class add basemodel property. called 'yourcontroller' above.

then should go ;-)

edit 3

just reminder namespace needs known if want use extension methods in view. best way achieve include namespace in web.config in views folder.

  <system.web.webpages.razor>     <host factorytype="system.web.mvc.mvcwebrazorhostfactory, system.web.mvc, version=3.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" />     <pages pagebasetype="system.web.mvc.webviewpage">       <namespaces>         <add namespace="system.web.mvc" />         <add namespace="system.web.mvc.ajax" />         <add namespace="system.web.mvc.html" />         <add namespace="system.web.routing" />         <add namespace="yournamespace" /> <- here       </namespaces>     </pages>   </system.web.webpages.razor> 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -