ASP. net mvc entry 5. View and viewdata

Source: Internet
Author: User
Tags blogengine

This seriesArticleBased on ASP. net mvc preview5.

View is the most direct contact with users in the MVC mode. It is responsible for data presentation. Note that the view is only responsible for data presentation, so we should try to make the view not involving the processing of business logic.

Let's add a view of the blog homepage. After ASP. net mvc is installed, we can see the MVC view template when adding a new project:

 
Note: If you are using the Chinese version of Vs, you may not be able to find this template after installation. For details, referInstall MVC in the Chinese version vs 08Set this article.

Among them, the MVC View content page has a master page. Add an MVC View content page under the views/home directory and select the site. master page under the views/shared directory:

Public   Partial   Class Index: viewpage
{
}

ASP. net mvc uses webform as the view by default. Therefore, we can see that the newly created ASPX page inherits from the viewpage. If the ASPX page is used as the view engine of ASP. net mvc, all aspx pages must inherit from the viewpage. Let's take a look at the viewpage:

We can see that viewpage inherits from the page of ASP. NET webform, implements the iviewdatacontainer interface, and provides some helper class instances. We can use viewdata to transmit data from the Controller to the view page. Next we will retrieve the posts list in index action of homecontroller and display it in view. We first extract data from the Controller. As mentioned above, we will directly use the model layer of blogengine as our 4mvcblog model for convenience. So ourCodeAs follows:

Public Actionresult index ( Int ? ID)
{
Viewdata [ " Title " ] = Blogsettings. instance. Name;

List<Ipublishable>Posts=Blogengine. Core. Post. Posts
. Convertall (NewConverter<Post, ipublishable>(Delegate(Post p ){ReturnPAsIpublishable ;}));

Int Pageindex = (ID ! =   Null   && Id. hasvalue && Id. Value >   0 ) ? Id. value: 1 ;
Int Pagesize = Math. Min (posts. Count, blogsettings. instance. postsperpage );
If (Pageindex -   1 ) * Pagesize + Pagesize > Posts. Count)
{
Return Showmsg ( New List < String > (){ " Page number out of range " });
}
Posts = Posts. getrange (pageindex -   1 ) * Pagesize, pagesize );

Viewdata ["Posts"]=Posts;//Transmit data to viewdata

//The view is returned to the client. If the name of the view to be returned is not specified,
//The view with the same name as the action is returned,
//That is, return view ("Index ");
ReturnView ();
}

The default webformview search order is as follows:

{1} is controllername, and {0} is actionname. Masterlocationformats is the search order of the master page.

In the above Code, we use viewdata ["posts"] To pass data to the view page, and then we can retrieve and present the data in the view to the user, views/home/index. some codes of the ASPX page are as follows:

As shown in the code in the red box above, we can retrieve the data from viewdata and convert it to the corresponding type. Here we find that viewdata needs to be converted to a type. In fact, we can set viewdata. model to a strong type. We only need to inherit the view page from viewpage <tModel>:

Then, when return view () in the Controller directly transmits the value to viewdata. model, as shown below:

Then, in the view, we can directly take the value from the strong viewdata. Model:

From the code above, we can see that viewdata. model is of the List <ipublishable> type and does not need to be converted.

Viewdata also has an eval method. We can use this method to take values from viewdata. Assume that I use return view (post) in action to pass the data of a log to view. While post has a previous attribute pointing to the previous log, we can set this value on the view page:

<% = Viewdata. eval ( " Previous. Title " ) %>

However, if you use the sample blog that I provided lastProgramIn this way, you can directly use "." In the value, and you will find that the value cannot be obtained. Blogengine implements the idataerrorinfo interface, while idataerrorinfo has an indexer. That is to say, business 8 has an indexer because it has an indexer, so that vertices cannot be used in eval (do not know if it is a bug ?).

Supplement: The above is not a bug because businessbase implements the idataerrorinfo interface, which has an index, resulting in viewdata. when the eval () method is called, string is returned when you search for the index value. empty to make viewdata. eval () is considered to have found the value and thus become invalid.

You can change return string. Empty to return null.

Well, this part is here first. Enjoy! Post by Q. Lee. Lulu.

Code of the sample blog in this article: 4mvcblog_5.rar

---------------------------------------------------------------------

For more information, follow the http://blog.51mvc.com/and http://bbs.51mvc.com/

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.