ASP.NET MVC 3: New @model keyword in Razor
page 5 of 7
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32183/ 53

Referencing the Model using the ASP.NET MVC 3 Beta and new @model syntax

We’ve added a new @model directive with the ASP.NET MVC 3 Beta that provides a cleaner and more concise way to indicate you want to use strongly-typed model classes within your view files.  You can now just write @model StrongModelType at the top of your Razor view file, and you do not need to have an @inherits or specify a view base class anymore:

image

The above syntax is conceptually the same as before (except with a lot fewer characters).  It is easier to read and type.

Below is what a complete Index.cshtml view implementation might look like to render our original screen-shot above:

image

One question you might ask is – so what does my view file derive from then if it isn’t specified?  By default, Razor will derive the view from the System.Web.Mvc.WebViewPage<TModel> base class.  You can optionally override this default base class (as well as the list of code namespaces that are imported by default within view files) by modifying the web.config file of your \Views directory.  This enables you to keep a clean (and DRY) syntax within your view files even if you have created a custom View base class that you want to use.

Note: Visual Studio Code/Markup Intellisense and Colorization within Razor files aren’t enabled yet with the Beta earlier this month.  You’ll see this show up in a few weeks though – and it will support full code intellisense for HTML, JavaScript, CSS and C#/VB code within Razor files.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 1:45:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search