The distribution of table data or other metric data on the website is a very common requirement, in the past, we had a GridView or DataPager that could help us to perform auto-segmentation, while ASP. net mvc all come over, but we don't have to really do the separation on our own, this is because someone has already helped us compile the program and open the original notebook to share it with the world. If you have already experienced. net mvc makes proper use of the Strong Typed feature for development, you will see how wonderful the development process is with Visual Studio 2008. Here is a simple example: You can define an Action Method in the Controller and obtain all the information that needs to be displayed in the View first, if you use LINQ to SQL, you can directly export the IQueryable object to the View, which is the Model used in the View, in this way, you can enjoy the effect of deferred Loading (Defered Loading.
View plaincopy to clipboardprint?
- Public ActionResult Index ()
- {
- IQueryable <Customer> custs =
- From cust in db. MERs
- Where cust. City = "Taiwan"
- Select cust;
- Return View (custs );
- }
After that, you can assign IQueryable <Customer> to the generic type when declaring a commitment in your View:
View plaincopy to clipboardprint?
- <% @ Page Language = "C #"
- Inherits = "System. Web. Mvc. ViewPage <IQueryable <Customer>" %>
Or an IEnumable <Customer> in simplified form, which is also a common usage:
View plaincopy to clipboardprint?
- <% @ Page Language = "C #"
- Inherits = "System. Web. Mvc. ViewPage <IEnumable <Customer>" %>
Then you can use foreach to retrieve all the information and display the information:
View plaincopy to clipboardprint?
- <Table>
- <% Foreach (var item in Model) {%>
- <Tr>
- <Td> <% = Html. Encode (item. ID) %> </td>
- <Td> <% = Html. Encode (item. Name) %> </td>
- <Td> <% = Html. Encode (item. Tel) %> </td>
- </Tr>
- <% }%>
- </Table>
This is the standard ASP. net mvc to obtain the information and demonstrate the Pattern in the View.