First time at GK Digital?

GK Digital is an online, collaborative learning platform built from the ground up to be a superior learning experience on the web. It is self-paced and supported by instructors and peers. GK Digital is professional training in the 21st century, from Global Knowledge.
Activity: Importing namespaces into Razor views

There is no disputing that Razor views are code-heavy. Of course that code should stay laser focused on turning data into HTML. But nonetheless you will write some code in your *.cshtml razor files.

MVC makes solid efforts to surface the key types in the razor views without resorting to typing out full namespace names. For example, you can type simply:

The expression is @name.GetExpressionText()

rather than the full namespace version:

The expression is @System.Web.Mvc.ExpressionHelper.GetExpressionText(name)

because the namespace System.Web.Mvc is imported. Great.

But what about your classes and models? You'd much rather write

@foreach (Customer c in Model.Customers) {}

than

@foreach (DevelopMentor.LearningLine.Ecommerce.Data.Customer c in Model.Customers) {}

This short blog post takes you through the options and steps of importing namespaces for commonly used types in your project. It'll save you tons of typing on big projects.

Note: There is a section where they talk about adding to the <pages> node in the web.config file. Be careful! MVC projects have at least two web.config files. This file they are talking about is not the web.config in your project root folder. It lives in the Views folder (i.e. [Project Name]\Views\web.config).


Readability view is recommended on this activity:

Image Text

Preview any course on GK Digital for free.

Just choose a course from our catalog and then press preview course for free.