Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



Learn Now


rssbus
 


Xojo

Reader rating:
Click here to read 1 comment about this article.
Article source: CoDe (2011 Mar/Apr)


Article Pages: < Previous - 1 2  3  4 5 - Next >


The Razor View Engine (Cont.)

Working with ViewData, the ViewBag and the Model

Accessing view data has never been simpler in ASP.NET MVC. To access the data within your view data dictionary you can simply perform the same steps as you would with any other view engine - supply the key to the dictionary to retrieve the value as shown below:

@{
    var name = ViewData["name"];
}

In the example above I’m retrieving the name object from the dictionary. This could be anything: a string, a complex type, or any other primitive.

While there is nothing new and exciting to talk about in regards to the view data dictionary, there is a new kid on the block - the ViewBag object. The ViewBag is a dynamic object which can be used to pass data from the controller to the view in the same manner in which you have set it up with the view data dictionary. What’s nice about the ViewBag object is that because of its dynamic nature you can set and get field values and add any number of additional fields without the need of strongly-typed classes. I think it’s best to see this in action. The example below shows how I’m setting up the ViewData object in a controller.

public ActionResult Foo()
   {
      ViewData["FirstName"] = "Donn";
      ViewData["LastName"] = "Felker";
      return View(); 
   }

Using the Razor view engine, I can pull these values out of the view data dictionary fairly easily with the following syntax.

In an inline code block:

public ActionResult Magazine()
{
    ViewData["FirstName"] = "Donn";
    ViewData["LastName"] = "Felker";
    return View(); 
}

Basic output notation:

@ViewData["FirstName"]
@ViewData["LastName"]

While this works, this same thing can be done a lot cleaner with the ViewBag. To utilize the ViewBag you simply set up the fields in the dynamic ViewBag object. At that point they are available to the rendering view via those same field names. To populate the ViewBag, set it up in the controller action as shown below:

public ActionResult Foo()
{
    ViewBag.FirstName = "Donn";
    ViewBag.LastName = "Felker";
    return View();
}

In the view, access the ViewBag fields directly. This will output the values to the screen as shown below. This is possible because of the ViewBag dynamic type.

Inline code:

@{
    var firstName = ViewBag.FirstName;
    var lastName = ViewBag.LastName; 
}

Basic output notation:

@ViewBag.FirstName
@ViewBag.LastName

While it may look confusing at first, it’s a much cleaner implementation because I am not depending on string literals to be the driving factor in how my view retrieves data.

You also have the option of providing multi-token statements to perform various operations within the view. One such example would be concatenating strings in the view as shown here:

@{ 
    var name = "Awesome";
}
<p>@("Hello Captain " + name)</p>

Last but certainly not least is the view model object. Strongly typing your views is actually quite simple. To set up your view to become strongly typed, include the model declaration at the top of your view as shown here:

@using CodeMagazine.Models
@model CodeMagazine.Models.LogOnModel
           
@{
    ViewBag.Title = "Hello" + Model.UserName;
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit 
<href="http://asp.net/mvc" title="ASP.NET MVC 
Website">http://asp.net/mvc</a>.
</p>

As soon as the model type is declared at the top of the view, the view is strongly typed and you can access the members of the view model from the Model keyword as shown on the fifth line in the code above. Assume that the value of Model.UserName is “Donn.” In the code above I am setting the title of the page to say “Hello Donn” using the strongly typed model object. You can also access the model anywhere in the page using the Razor notation as shown here:

Hello, @Model.UserName, how are you? 

Expressions

If you’re familiar with the WebForm’s view engine implementation of if blocks and loops, then fortunately you will notice that there are not too many changes in regards to expressions with Razor.

If blocks are created inline within the HTML. While the implementation is not as versatile as other view engines, namely Spark, it’s still cleaner and more readable than the WebForms. A simple if block expression is shown below:

@if (Model.IsLoggedIn) {
    <p>You're logged in!</p>
else {
    <p>You're not logged in.</p>
}

The loop expression in Razor is much more readable and user friendly than its WebForm processor. For example, here is a for loop implementation using the WebForms view engine:

<ul id="current-products">
@foreach(var item in Model.Products) {

   <li>
   @item.Name <br/>
   @item.Upc <br/>
   <img src="@item.QrCodeUrl" border="0" />
   </li>
}
</ul>

In this example I’m looping over a list of products in the model object of the view. I’m outputting the name, Upc and QR Code Url of the product. Notice how Razor does not require the explicit closing of Razor notation; it is smart enough to figure out where it starts and when it should stop.

There is something interesting to note about how Razor renders content to the screen. When you render content from an if/else block, foreach or any other block construct you should wrap the content within HTML to better identify what is the beginning and end of the content block. For example, you might have some code that looks like this:

@if (Model.Count() > 0){
    <span>
        You've got data!
    </span>
}

This code will output to the HTML:

<span>
    You've got data!
</span>

However, there are instances in which you do not want to wrap your markup with another tag. To alleviate this you can wrap your content in the <text> tag to escape this as shown below:

@if (Model.Count() > 0){
    <text>
        You've got data!
    </text>
}

This code will output to the HTML:

You’ve got data!
&


Article Pages: < Previous - 1 2  3  4 5 - Next Page: 'Organizing Code with Layouts and Sections' >>

Page 1: The Razor View Engine
Page 2: Razor Fundamentals
Page 3: Working with ViewData, the ViewBag and the Model
Page 4: Organizing Code with Layouts and Sections
Page 5: Using Declarative HTML Helpers

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.6 out of 5

9 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Sharepoint TechCon

 

RssBus