Home > ASP.NET MVC > ASP.NET # MVC # 2 – Asp.net MVC Views :-(Part I)

ASP.NET # MVC # 2 – Asp.net MVC Views :-(Part I)

 

The view is responsible for providing user interface (UI) to the user.

 

In Asp.net mvc there are two types of view:-

1) Untyped or simple views :-

2) Strongly Typed Views :-

 

Today we will see Untyped or simple views :-

 

1)Untyped or simple views :-

 

Lets take an example for the view:-

<%@ Page Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master” 
Inherits
=”System.Web.Mvc.ViewPage” %> 
<asp
:Content ID=”indexTitle” ContentPlaceHolderID=”TitleContent” runat=”server”> 
Home Page 
<
/asp:Content> 
<asp
:Content ID=”indexContent” ContentPlaceHolderID=”MainContent” runat=”server”> 
<h2
><%: ViewData[“Message”] %></h2> 
<p
> 
To learn more about ASP
.NET MVC visit <a href=”http://asp.net/mvc” 
title
=”ASP.NET MVC Website”>http://asp.net/mvc</a>. 
<
/p> 
<
/asp:Content>
 
 
Technically, this is not a Web Form because it doesn’t include the <form runat=”server”> tag.
 
*Now we will see one of the method in controller that will return above view.
 
As we have seen in our previous posts like in controller we have methods those have their return types as ActionResult action result is a return type by using which we can return the above view.
 
For Ex.
 

public class HomeController : Controller
{
public ActionResult Index()
{
ViewData[“Message”] = “Welcome to ASP.NET MVC!”;
return View();
} 
}

 

 

 

This method ought to look familiar; it’s the Index action method of HomeController in the default
project template. Because the View name was not specified, the ViewResult returned by this
method looks for a View named the same as the action name in the /Views/ControllerName directory.

The View selected in this case would be /Views/Home/Index.aspx.

*As with most things in ASP.NET MVC, this convention can be overridden. Suppose that you want
the Index action to render a different View. You could supply a different View name like so:

 public ActionResult Index()
       
{
           
ViewData["Message"] = "Welcome to ASP.NET MVC!";
           
return View("NotIndex");
       
}

In this case, it will still look in the /Views/Home directory, but choose NotIndex.aspx as the View.

In some situations, you might even want to specify a View in a completely different directory structure.
You can use the tilde syntax to provide the full path to the View like so:

 public ActionResult Index()
       
{
           
ViewData["Message"] = "Welcome to ASP.NET MVC!";
           
return View("~/Some/Other/View.aspx"); 

        }

We will see the strongly typed view in the next blog.

Thank You.

Advertisements
Categories: ASP.NET MVC
  1. June 30, 2011 at 5:44 am

    Hi Ganesh, u hav given a gud intro of MVC. hope u’ll continue by posting more info on MVC..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: