0

On My project i'm using ASP.NET MVC3, and my backgound comes from traditional asp.net, my question is related "comparing with asp.net webforms" with the inheritance that is possible on codebehind.

What I intend (in mvc3) is to have a couple of extra properties available on all views of the application.

Is it possible to add my extra properties to my views or I need to use the "Helper way" as a work around?

Notes that my question is not at the model level, but for the view (UI).

Sorry forgot to mention that my viewengine is Razor

2
  • I'm not clear on what your asking. Are you asking if it's possible to subclass Controller add some properties to the new base class, and then derive your controllers from the new base class? If so, then yes its very possible. Commented Feb 24, 2012 at 18:56
  • Depends on what you mean by "extra properties". Commented Feb 24, 2012 at 19:10

2 Answers 2

1

If your extra properties are meant to be UI specific, then you can define a master page and use that on all of your view pages. If they are meant to be value properties, then you can create a base model and inherit that in your model classes, which then will be available to all of your views using those models. If none of these are feasible for your purpose, then you might try extending System.Web.Mvc.ViewPage, but I am not sure if it would be a best practice.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, by default MVC Views inherit System.Mvc.ViewPage. If you are using a strongly typed view, then it's System.Mvc.ViewPage<model> where your @model declaration is pass to the generic ViewPage<> class.

While it's possible to inherit from a derived ViewPage, it's not typically done. Depending on what you want to do, there is likely a better solution.

Perhaps if you clarified what you are doing, it might help us help you more.

2 Comments

thanks in advance, forgot to mention that i'm using razor. and imagine that i always have some value on my Session(for example), what i pretend is to get a way to get a property to do something like: somestring @MySessionProp somestring
It's not a good idea to access session values from Views. This breaks the seperation of View and Controller. The Session is accessed from the controller. If you need it in the view, pass it to the view intentionally, don't just let all session values be available in your view.. For example, pass the value as a ViewBag or ViewData object, or even add it to your model. The reason this is a bad idea is that it makes the view untestable. I know you're probably not doing unit testing, but the same things you do for unit testing makes your code more maintainable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.