Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need this 'common data' across all views, I would not throw inheritance in the mix. I would go for an extension method.</p> <p>Something along the lines of:</p> <pre><code>public static CommonData GetCommonData(this HtmlHelper htmlhelper) { // do your magic here } </code></pre> <p>In your View you could then simply call:</p> <pre><code>@Html.GetCommonData()... </code></pre> <p>That way you keep your models cleaner.</p> <p>Inheritance might be a valid approach, if this Common Data really 'fits' well with your Model classes.</p> <p>It would help if you provided some examples ;-)</p> <p>** EDIT **</p> <p>I would keep your 'BaseModel' as a property in the Controller and populate it on OnActionExecuting as you suggest.</p> <p>But I would use ExtensionMethods on the Controller and Html helper to access the the data. If you use inheritance you will needlessly complicate things.</p> <p>Let me know if you need samples of the extension methods.</p> <p>This approach is DRY. You are not duplicating code.</p> <p>** EDIT 2 **</p> <p>Here is how your extension method should look like: (off the top of my head, so it might not compile)</p> <pre><code>public static class ExtensionHelpers { public static BaseModel GetCommonData(this HtmlHelper htmlhelper) { YourController HtmlHelper = htmlhelper.ViewContext.Controller as YourController; if (controller != null) { return HtmlHelper.BaseModel; } return null; } </code></pre> <p>Make sure the Controller derives from the Controller class where you add the BaseModel property. I called it 'YourController' above.</p> <p>Then you should be good to go ;-)</p> <p>Edit 3</p> <p>Just a reminder that the namespace needs to be known if you want to use extension methods in a View. The best way to achieve this is to include the namespace in the Web.Config in your Views folder.</p> <pre><code> &lt;system.web.webPages.razor&gt; &lt;host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt; &lt;pages pageBaseType="System.Web.Mvc.WebViewPage"&gt; &lt;namespaces&gt; &lt;add namespace="System.Web.Mvc" /&gt; &lt;add namespace="System.Web.Mvc.Ajax" /&gt; &lt;add namespace="System.Web.Mvc.Html" /&gt; &lt;add namespace="System.Web.Routing" /&gt; &lt;add namespace="Yournamespace" /&gt; &lt;- LOOK HERE &lt;/namespaces&gt; &lt;/pages&gt; &lt;/system.web.webPages.razor&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload