The purpose of this tutorial is to  provide you with a brief introduction to ASP.NET MVC views, view data,  and HTML Helpers. By the end of this tutorial, you should understand  how to create new views, pass data from a controller to a view, and  use HTML Helpers to generate content in a view.

  Understanding Views

  For ASP.NET or Active Server Pages,  ASP.NET MVC does not include anything that directly corresponds to a  page. In an ASP.NET MVC application, there is not a page on disk that  corresponds to the path in the URL that you type into the address bar  of your browser. The closest thing to a page in an ASP.NET MVC application  is something called a view.

  ASP.NET MVC application, incoming  browser requests are mapped to controller actions. A controller action  might return a view. However, a controller action might perform some  other type of action such as redirecting you to another controller action.

  Listing 1 contains a simple controller  named the HomeController. The HomeController exposes two controller  actions named Index() and Details().

  Listing 1  - HomeController.cs

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
} public ActionResult Details()
{
return RedirectToAction("Index");
}
}
}

  You can invoke the first action, the  Index() action, by typing the following URL into your browser address  bar:

/Home/Index

  You can invoke the second action, the  Details() action, by typing this address into your browser:

/Home/Details

  The Index() action returns a view.  Most actions that you create will return views. However, an action can  return other types of action results. For example, the Details() action  returns a RedirectToActionResult that redirects incoming request to  the Index() action.

The Index() action contains the following  single line of code: View();

  This line of code returns a view that  must be located at the following path on your web server:

\Views\Home\Index.aspx

  The path to the view is inferred from  the name of the controller and the name of the controller action.

  If you prefer, you can be explicit  about the view. The following line of code returns a view named  Fred :

  View( Fred );

  When this line of code is executed,  a view is returned from the following path:

\Views\Home\Fred.aspx

  Adding Content  to a View

  A view is a standard (X)HTML document  that can contain scripts. You use scripts to add dynamic content to  a view.For example, the view in Listing 2  displays the current date and time.

  Listing 2  - \Views\Home\Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Index</title>
</head>
<body>
<div> The current date and time is
<% Response.Write(DateTime.Now);%> </div>
</body>
</html>

 Notice that the body of the HTML page  in Listing 2 contains the following script:

<% Response.Write(DateTime.Now);%>

You use the script delimiters <%  and %> to mark the beginning and end of a script. This script is  written in C#. It displays the current date and time by calling the  Response.Write() method to render content to the browser. The script  delimiters <% and %> can be used to execute one or more statements.

Since you call Response.Write() so  often, Microsoft provides you with a shortcut for calling the Response.Write()  method. The view in Listing 3 uses the delimiters <%= and %> as  a shortcut for calling Response.Write().

  Listing 3 - Views\Home\Index2.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Index</title>
</head>
<body>
<div> The current date and time is
<%=DateTime.Now %> </div>
</body>
</html>

  You can use any .NET language to generate  dynamic content in a view. Normally, you�ll use either Visual Basic  .NET or C# to write your controllers and views.

  Using HTML Helpers  to Generate View Content

  You use view data to pass data from  a controller to a view. Think of view data like a package that you send  through the mail. All data passed from a controller to a view must be  sent using this package. For example, the controller in Listing 6 adds  a message to view data.

  Listing 6 - ProductController.cs

  

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
public class ProductController : Controller
{
public ActionResult Index()
{
ViewData["message"] = "Hello World!";
return View();
} }
}

  The controller ViewData property represents  a collection of name and value pairs. In Listing 6, the Index() method  adds an item to the view data collection named message with the value  Hello World! . When the view is returned by the Index() method,  the view data is passed to the view automatically.

  The view in Listing 7 retrieves the  message from the view data and renders the message to the browser.

  Listing 7 -- \Views\Product\Index.aspx

  

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Product Index</title>
</head>
<body>
<div> <%= Html.Encode(ViewData["message"]) %> </div>
</body>
</html>

  Notice that the view takes advantage  of the Html.Encode() HTML Helper method when rendering the message.  The Html.Encode() HTML Helper encodes special characters such as <  and > into characters that are safe to display in a web page. Whenever  you render content that a user submits to a website, you should encode  the content to prevent JavaScript injection attacks.

  (Because we created the message ourselves  in the ProductController, we don't really need to encode the message.  However, it is a good habit to always call the Html.Encode() method  when displaying content retrieved from view data within a view.)

  In Listing 7, we took advantage of  view data to pass a simple string message from a controller to a view.  You also can use view data to pass other types of data, such as a collection  of database records, from a controller to a view. For example, if you  want to display the contents of the Products database table in a view,  then you would pass the collection of database records in view data.

  You also have the option of passing  strongly typed view data from a controller to a view. We explore this  topic in the tutorial Understanding Strongly Typed View Data and  Views.

  Summary

  This tutorial provided a brief introduction  to ASP.NET MVC views, view data, and HTML Helpers. In the first section,  you learned how to add new views to your project. You learned that you  must add a view to the right folder in order to call it from a particular  controller. Next, we discussed the topic of HTML Helpers. You learned  how HTML Helpers enable you to easily generate standard HTML content.  Finally, you learned how to take advantage of view data to pass data  from a controller to a view.

  原文网址:http://www.asp.net/mvc/tutorials/older-versions/views/asp-net-mvc-views-overview-cs

ASP.NET MVC- VIEW Overview Part 1的更多相关文章

  1. 【记录】ASP.NET MVC View 移动版浏览的奇怪问题

    ASP.NET MVC View 中的一段代码: <span id="span_Id">@Model.ID</span> 没什么问题吧,浏览器浏览正常,查看 ...

  2. ASP.Net MVC View

    ASP.Net MVC View(视图)   View视图职责是向用户提供界面.负责根据提供的模型数据,生成准备提供给用户的格式界面. 支持多种视图引擎(Razor和ASPX视图引擎是官方默认给出的, ...

  3. ASP.NET MVC View 和 Web API 的基本权限验证

    ASP.NET MVC 5.0已经发布一段时间了,适应了一段时间,准备把原来的MVC项目重构了一遍,先把基本权限验证这块记录一下. 环境:Windows 7 Professional SP1 + Mi ...

  4. ASP.NET MVC View中的标签(tag)

    在编辑View的时候会用到各种HTML标签,如<a>,<input>,<p>等待,这些标签在ASP.NET MVC中都有对应的编程语法,它叫Razor,它是帮助我们 ...

  5. ASP.NET MVC View向Controller传值方式总结

    1:QueryString传值1)也可以使用new{}来为form的action增加querystring2)在controler里使用Request.QueryString["word&q ...

  6. ASP.NET MVC View向Controller提交数据

    我们知道使用MVC的一个很重的的用途就是把Controller和View之间进行解耦,通过控制器来调用不同的视图,这就注定了Controller和View之间的传值是一个很重的知识点,这篇博文主要解释 ...

  7. ASP.NET MVC View使用Conditional compilation symbols

    由于View(.cshtml)的运行时编译关系,在项目级别中定义的symbols是无法被直接使用的.需要在Web.config中添加compilerOptions(在View目录下的Web.confi ...

  8. ASP.NET MVC view引入命名空间

    两种方式:1,在cshtml中引入@using Admin.Models 2,在 Views 文件夹中的 Web.config 文件中添加引用如: <pages pageBaseType=&qu ...

  9. probing privatePath如何作用于ASP.NET MVC View

    当View上using一些从probing privatePath加载的程序集,运行时会提示无法找到对应程序集. <runtime> <assemblyBinding xmlns=& ...

  10. asp.net mvc View视图相关

    1.0 @helper语法 @helper语法可以定义可重复使用的帮助器方法: 例如 @helper methodName(type paramName,...){ //todo } 调用:@meth ...

随机推荐

  1. 商务通简单弹窗样式 V1.0

    代码为: document.writeln('<style>*{margin:0; padding:0;}</style>');//创建中间弹框    document.wri ...

  2. Day9 网络编程

    OSI(Open System Interconnect),即开放式系统互联. ISO(International Standards Organization)国际标准化组织 OSI七层模型: TC ...

  3. C++ static全局变量与全局变量的区别/static全局函数与全局函数的区别

    全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...

  4. 面试题:m个长度为n的ordered array,求top k 个 数字

    package com.sinaWeibo.interview; import java.util.Comparator; import java.util.Iterator; import java ...

  5. UITableView中复用cell显示信息错乱

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  6. Github readme语法-- markdown

    README 该文件用来测试和展示书写README的各种markdown语法.GitHub的markdown语法在标准的markdown语法基础上做了扩充,称之为GitHub Flavored Mar ...

  7. 我的pch文件

    /** *  1. RGB背景色 */ #define PPCOLOR_RGB(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue: ...

  8. 三菱plc编程电缆通讯端口设置方法(转载)

    三菱plc编程电缆通讯端口如何设置?三菱plc编程电缆通讯端口设置方法 时间:2015-10-21 05:09:20编辑:电工栏目:三菱plc 导读:三菱plc编程电缆通讯端口的设置方法,三菱plc上 ...

  9. 用Thread类创建线程-2

    支持原创,本系列文章均转自:http://www.blogjava.net/nokiaguy/category/38172.html 在Java中创建线程有两种方法:使用Thread类和使用Runna ...

  10. uva 1400 - "Ray, Pass me the dishes!"

    又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...