My View inherits Models.MyModel
<%@ Page Language="C#" MasterPageFile="Something.Master" Inherits="Models.MyModel>" %>
I need a property Model.Something to be available in a HtmlHelper method when I call it from this view.
<%= Html.CustomHelper(...) %>
Is there any way to access this? Maybe via ViewContext or ViewDataDictionary?
I do not want to explicitly pass Model.SessionKey for each helper I call. Is there any approach that I missed? Or is this impossible.
Thanks.
My approach to this would be to have the all the models that you want to use with this helper implement an interface that defines their common properties. The ViewData property on the HtmlHelper object has a Model property (of type
object). Inside your helper, you can cast this as the interface type. Assuming that it is non-null at that point, i.e., actually not null and of the correct type, you can then use the common properties.