ASP. net mvc 3.0 (6): Create your view in MVC 3.0

Source: Internet
Author: User

ASP. net mvc 3.0 (I): summary of the new features of MVC 3.0

ASP. net mvc 3.0 (II): MVC concept and MVC 3.0 development environment

ASP. net mvc 3.0 (III): first recognized mvc url ing rules Routing

ASP. net mvc 3.0 (4): I want to configure routing for MVC rules.

ASP. net mvc 3.0 (V): Start With Controller/Action

ASP. net mvc 3.0 (6): Create your view in MVC 3.0

ASP. net mvc 3.0 (7): The New razor engine of MVC 3.0

ASP. net mvc 3.0 (8): passing and saving your model in MVC 3.0

ASP. net mvc 3.0 (9): MVC 3.0 verifies your model

ASP. net mvc 3.0 (10): MVC 3.0 uses forms authentication

ASP. net mvc 3.0 (11): Use filter in MVC 3.0

ASP. net mvc 3.0 (12): MVC 3.0 uses custom HTML controls

ASP. net mvc 3.0 (13): MVC 3.0 prevents cross-site Request Forgery (csrf) Attacks

ASP. net mvc 3.0 (14): create a data table in the MVC 3.0 instance Series

ASP. net mvc 3.0 (15th): Sorting of tables in the MVC 3.0 instance Series

ASP. net mvc 3.0 (16): pages of table data in the MVC 3.0 instance Series

ASP. net mvc 3.0 (17): filtering data in tables of MVC 3.0 instances

ASP. net mvc 3.0 (18): Combined sorting, paging, and filtering of tables in the MVC 3.0 instance Series

ASP. net mvc 3.0 (19th): using open-source controls for MVC 3.0 instances to sort and pagination tables

 

Overview

 

In the Model-View-controller (MVC) mode, a view is used to encapsulate the presentation logic. These views should not contain any application logic or database retrieval code. All application logic should be processed by the Controller. The view uses the data transmitted from the Controller to present the corresponding UI. This data is transmitted from the Controller Operation Method to the view using the view method.

In a common workflow of an MVC web application, the Controller operation method processes incoming Web requests.

These operations use input parameter values to execute application code and retrieve or update data model objects in the database. Then, these methods will select a view that presents the response to the browser.

 

MVC 3.0 view Engine

 

MVC 3.0 provides two view engines: aspx (C #) and razor (cshtml)

Aspx (C #) Engine

The ASP. net mvc framework allows you to use the view engine to generate a view (UI ). By default, the MVC framework uses. NET page (. aspx), master page (. master) and user controls (. the custom types inherited from the ascx type (viewpage, viewmasterpage, and viewusercontrol) are used as views.

Razor (cshtml) Engine

 

_ Appstart. cshtml:

The application is executed after the global. application_start method when it is started.

Content to be processed during app initialization. For example, some information initialized to the database is recorded.
Function and global. application_start is similar. The difference is that the global start is executed first and then to this _ appstart. It is worth noting that it can be used in the _ appstart context. new Dynamic Features of net4 ~~ The Declaration is a type of attribute, field, indexer, parameter, return value, or type constraint.

_ Layout. cshtml:

The layout page is similar to the master page of Aspx.

Create an MVC view (razor engine)

 

Create an empty MVC 3.0 Application Based on the razor Engine

Select an empty project template and razor view Engine

 

Name a new controller homecontroller

Right-click the Controller folder

Enter the Controller name. Note that it must end with the Controller.

After adding the controller, the index method is provided by default...

In this case, the view corresponding to the action is not created, so the view is red... An error is reported on the runtime interface ..

 

public ActionResult Index()
{
return View();
}

 

Let's first output a string to the page...

Code

 

public ActionResult Index()
{
return View();
}

 

Change

 

publicstring Index()
{
return"Hello World";
}

 

After running, a simple interface is displayed...

 

 

Add View

 

At this time, we simply show some strings. However, our interface still requires many elements, not just text ..

So we have to start with the view ..

Return to the previous

public ActionResult Index()
{
return View();
}

Modify the previous code to make the Controller index return an actionresult.

For actionresult, see ASP. net mvc 3.0 (5): Start Controller/action.

Right-click the action pop-up form and select add view.

We recommend that you do not modify the view name, which will be explained later.

In this case, a view corresponding to the action is generated for the Controller in the view folder.

Compile HTML code in the view

 

@{
Viewbag. Title = "Homepage ";
}

<H2> <a href = "Index"> Hello World </a> </H2>
Running Effect

  Data transmitted between the Controller and viewIn many cases, we need to pass some data to the interface. Just as the web session code is as follows:
public ActionResult Index()
{
ViewData["Message"] ="Hello World";
return View();
}

View

@{
Viewbag. Title = "Homepage ";
}

<H2> <a href = "Index"> @ viewdata ["message"] </a> </H2>

The running effect is still the same...

About tempdata, viewbag, and viewdata

 

In fact, I personally think they are all the same.

If you call tempdata and viewdata in viewbag mode, the same effect will apply.

And vice versa...

For example, viewbag. Message and viewdata ["message"]

public ActionResult Index()
{
ViewBag.Message ="Hello World";
// ViewData["Message"] = "Hello World";
return View();
}

The Running Effect of view code remains the same... The effects of repeated debugging are the same ..

Similarities and differences between tempdata, viewdata, and viewbag:

 

Tempdata viewdata viewbag can be used to save data. The differences between them are as follows: tempdata: stored in the session. Each time the Controller executes a request, it first obtains tempdata from the session, and then clears the session to obtain tempdata, although saved in an internal dictionary object, each entry in its set is deleted from the dictionary table once accessed. At the specific code level, the tempdata acquisition process is to read data from the session of controllercontext through the sessionstatetempdataprovider. loadtempdata method, and then clear the session. Therefore, tempdata can be transferred only once across the controller. Viewdata: the lifecycle is the same as that of the view. It is only valid for the current view. Viewbag: The same as the life cycle of viewdata. It is also valid for the previous view. The difference is that the viewbag type is not the dictionary key-Value Pair structure, but the dynamic type, added in mvc3. SummaryAt the beginning, we couldn't make a summary. I will try again later .. (* ^__ ^ *) Next PreviewNew razor: Memories of lost youth  Http://www.cnblogs.com/lukun/

 

Tempdata viewdata viewbag can be used to save data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.