Introduction to ASP. 5, View and ViewData

Source: Internet
Author: User
Tags blogengine

Original address: http://www.cnblogs.com/QLeelulu/archive/2008/10/04/1303913.html

View has the most direct contact with the user in the MVC pattern, which is responsible for rendering the data. One thing to note here is that view is only responsible for the presentation of the data, so we should try to keep the view from dealing with the business logic.

Let's add a view of the blog home page. After installing ASP. NET MVC, we can see the MVC view template when adding a new project:


Note: If you are in the Chinese version of VS, after the installation may appear to find this template phenomenon, you can refer to install MVC in the Chinese version vs 08 This article is set up a bit.

Where the MVC View Content page has a master page. We add an MVC View Content page under the Views/home directory and select the Site.master master page in our views/shared directory:

Public Partial classIndex:viewpage
{
}

The default for ASP. NET MVC is to use WebForm as the view. So we see that the new ASPX page inherits from ViewPage, and if you use an ASPX page as the view engine for ASP., all ASPX pages must inherit from ViewPage. Let's look at ViewPage again:

We see that viewpage inherits from the page page of ASP. WebForm, implements the Iviewdatacontainer interface, and also provides examples of helper classes. We can use ViewData to pass data from the controller to the view page. Below we take the posts list in the index action in HomeController and then display it in view. We first take out the data in the controller, said earlier, for convenience, we will directly use the BlogEngine model layer as our 4mvcBlog model. So our code is as follows:

PublicActionResult Index (int?ID)
{
viewdata["Title"]=BlogSettings.Instance.Name;

List<ipublishable>posts=BlogEngine.Core.Post.Posts
. ConvertAll (NewConverter<Post, Ipublishable>(Delegate(Post p) {returnP asipublishable;}));

intPageIndex=(ID!= NULL &&ID. HasValue&&ID. Value> 0)?ID. Value:1;
intpageSize=Math.min (posts. Count, BlogSettings.Instance.PostsPerPage);
if((PageIndex- 1)*pageSize+pageSize>posts. Count)
{
returnShowMsg (NewList<string>() { "page number out of range" });
}
Posts=posts. GetRange ((PageIndex- 1)*pageSize, pageSize);

viewdata["Posts"]=posts;//passing data to the ViewData

//This returns the view to the client, if you do not specify the name of the view to return,
//is to return a view with the same name as action,
//That is, the equivalent of return View ("Index");
    returnView ();
}

The order of the default webformview search view is searched in the following order:

where {1} is controllername,{0} is actionname. Masterlocationformats the search order for the master page.

In the above code we use viewdata["Posts"] to pass the data to the view page, and then we can take the data and present it to the user in the view, and some of the code for the Views/home/index.aspx page is as follows:

As the code in the red box, we can extract the data from the ViewData and convert it to the appropriate type. Here we find ViewData to do a type of conversion, in fact, we can set Viewdata.model to strong type, just to inherit our view page from Viewpage<tmodel> can be:

Then return the View () in the controller, directly to the Viewdata.model value, as follows:

Then in view we can take the value directly from the strongly typed Viewdata.model:

I can see from the above code that Viewdata.model is the list<ipublishable> type and does not require any further type conversions.

ViewData also has an eval method that we can use to take values from the ViewData. If I use return view (Post) in action, pass a log of data to view. While the post has a previous attribute pointing to the previous log, we can take the value in the View page:

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

But if I use the code of the example blog program I provided last time, I use "." directly in the value. To fetch the value, you will find that it is not worth it. Because the Businessbase class in BlogEngine implements the IDataErrorInfo interface, and IDataErrorInfo has an indexer, which means that Businessbase has an indexer, because there is an indexer, Make it impossible to use points to get a value in eval (not sure if it is a bug?). )。

Add: The above is not a bug, because Businessbase implements the IDataErrorInfo interface, which has an indexer that causes viewdata.eval () When a method call searches for the value of an indexer, it returns String.Empty and causes Viewdata.eval () to assume that the value is found, thereby invalidating it.

We can return a string. Empty is modified to return NULL, so it is possible.


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.