Home > Article > Backend Development > Detailed introduction to ASP.NET MVC--View
Understanding Views
ASP.NET MVCUnlike ASP.NET or Dynamic Server Pages (ASP), it does not have anything directly corresponding to a page thing. In an ASP.NET MVC application, there is no page on disk that corresponds to the URL path you enter in the browser address bar. In an ASP.NET MVC application, the closest thing to a page is something called a view. In an ASP.NET MVC application, incoming browser requests are mapped to controller actions. A controller action may return a view. However, a controller action may perform some type of action, such as redirecting you to another controller action.
Code Listing 1 contains a simple controller called HomeController. HomeController exposes two controller actions called index() and details().
代码清单1 - HomeController.cs 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.Mvc; 命名空间 MvcApp.Controllers{ [HandleError] public class HomeController:Controller { public ActionResult Index() { return View(); } public ActionResult Details() { return RedirectToAction( “Index”); } }}
You can call the first action, the index() action, by entering the following URL in the browser's address bar:
/首页/index
You can The second action, the details() action, is called by entering this address in the browser:
/Homepage/Details
The index() action returns a view. Most actions you create will return a view, however, actions can return any type of action result. For example, the details() action returns a RedirectToActionResult, which can redirect incoming requests to the index() action.
The index() action contains the following line of code:
return View();
This line of code returns a view, and the path of the view on the server must be Same as the following path: Index.aspx
\View\Home\\
The path to the view is inferred from the names of the controller and controller actions.
If you wish, you can explicitly specify the view. The following line of code returns a view named "Fred":
Return View("Fred");
When this line of code is executed, a view will be returned from the following path:
\View\Home\Fred.aspx
2. Create a view
You can right-click on the folder in the solution browser and select the menu item "Add", "New Project" (Figure 1). Select the "MVC View Page" template to add standard views to your project.
You should be aware that you cannot add views to your project at will like you can in ASP.NET or ASP applications. You have to add the view to a folder with the same name as the controller (without the controller suffix) For example, if you want to create a new view called index, the view can be created by A controller named ProductController is returned, then you must add this view to the following folder of the project:
\View\Product\Index.aspx
The folder containing the view The name must correspond to the name of the controller that returns the view.
3. Add content to the view
A view is a standard (X)
HTML documentthat can contain scripts. You use scripts to add dynamic content to views. For example, the view in Listing 2 displays the current date and time.
Code Listing 2 - \Views\Home\Index.aspx
<%@ Page Language =“C#”AutoEventWireup =“true”CodeBehind =“Index.aspx.cs”Inherits =“MvcApp.Views.Home.Index”%> <!DOCTYPE html PUBLIC“ - // W3C // DTD XHTML 1.0 Transitional // EN“”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“> <头RUNAT = “服务器”>索引 TITLE> HEAD> 的当前日期和时间是: <%回复于(DateTime.Now);%> DIV> body> html>Notice that the body of the HTML page in Code Listing 2 contains the following script:
<%Response.Write(DateTime.Now);%>Use Script delimiters mark the beginning and end of a script. This script is written in C#. It displays the current date and time, rendering the content to the browser script by calling the reply() method. The delimiters can be used to execute one or more statements.
Because the reply to () method is often called, Microsoft provides you with a simple way to call the reply to () method. The view in Listing 3 uses as a simple way to call the reply() method.
Code Listing 3 - Views \ Home \ Index2.aspx
<%@ Page Language =“C#”AutoEventWireup =“true”CodeBehind =“Index2.aspx.cs”Inherits =“MvcApp.Views.Home.Index2”%> <!DOCTYPE html PUBLIC“ - // W3C // DTD XHTML 1.0 Transitional // EN“”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“> <头RUNAT = “服务器”>索引2 TITLE> HEAD> 的当前日期和时间是: <%= DateTime.Now%> DIV> BODY> < / HTML>You can use any .NET language to generate dynamic content in the view, you can use Visual Basic.Net or C# to write your controllers and views.
4. Use HTML Helpers to generate view content
为了使向视图中添加内容更加容易一些,你可以利用叫做HTML Helper的东西.HTML Helper是一个生成字符串的方法。你可以使用HTML帮助者来生成标准的HTML元素,例如文本框,链接,下拉框和列表框。
举个例子,代码清单4中的视图利用了两个HTML Helpers,TextBox()和Password(),用于生成一个登录窗体(见图2)。
代码清单4 - \ Views \ Home \ Index3.aspx
<%@ Page Language =“C#”AutoEventWireup =“true”CodeBehind =“Index3.aspx.cs”Inherits =“MvcApp.Views.Home.Index3”%> <!DOCTYPE html PUBLIC“ - // W3C // DTD XHTML 1.0 Transitional // EN“”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>登录表单 title> head>
Previous article:Detailed introduction to ASP.NET MVC-routingNext article:Detailed introduction to ASP.NET MVC-routing