1. List page. Generally, List pages are used to display data. The List page in this article provides data display and paging, deletion, addition, and modification operations. Because some people in the garden are still using ViewData when using MVC for data display, here we will introduce strong-type display data. Add a new view. In the first line of code, you can see
Copy codeThe Code is as follows:
<% @ Page Language = "C #" Inherits = "System. Web. Mvc. ViewPage <dynamic>" %>
The type of this view can be defined here, which is convenient to operate.
Copy codeThe Code is as follows:
<% @ Page Language = "C #" Inherits = "System. Web. Mvc. ViewPage <Web. Models. PageData <Web. Models. Users>" %>
The returned object class is a paging set object class, which defines the object class.
Copy codeThe Code is as follows:
/// <Summary>
/// Query the record set by PAGE
/// </Summary>
/// <Typeparam name = "T"> generic type </typeparam>
Public class PageData <T>
{
/// <Summary>
/// Obtain or set the paging record set returned by the query
/// </Summary>
Public List <T> GetDate = new List <T> ();
/// <Summary>
/// Obtain or set the total number of records that meet the query Conditions
/// </Summary>
Public int Count {get; set ;}
/// <Summary>
/// Number of entries per page
/// </Summary>
Public int PageSize = 5;
/// <Summary>
/// Current number of pages
/// </Summary>
Public int PageIndex {get; set ;}
}
After processing by the Controller, relevant parameters are returned, including the displayed data, total number of records, page number, and current page information required by the page. Because the type has been defined in the List page, you can call it directly through the Model.
It can be seen that the GetDate attribute stores a generic data set, so that we can display it cyclically. If the page is paginated, pass the value to the Controller Based on the page number, re-obtain the data, fill the data, and then return the data, and then display it. Here, you can display the data and pagination. Here we will introduce the pagination plug-in with jquery. pagination. If you are not familiar with the plug-in, there will be a detailed article in the garden.
The Controller obtains the relevant data from the database and fills it into the object class, so that the view can be called directly. The Writing Method in the write controller is described here.
Copy codeThe Code is as follows:
$ (Function (){
// Set paging Parameters
$ ("# Pagination"). pagination (<% = Model. Count %> ,{
Callback: pageselectCallback,
Prev_text: "«previous page", // text of the previous page button
Next_text: "Next page»", // next page button text
Items_per_page: <% = Model. PageSize %>, // How many entries are displayed on each page
Num_display_entries: 5, // Number of pagination entries displayed in the continuous pagination body section
Current_page: <% = Model. PageIndex %>, // current number of pages
Num_edge_entries: 1, // number of entries displayed on both sides of the page
Link_to :"? Page =__ id __"
});
});
Function pageselectCallback (page_id, jq ){
// Callback function
}
2. Add operation. First, we define an object class.
Copy codeThe Code is as follows:
/// <Summary>
/// User ID
/// </Summary>
Public int UID {get; set ;}
/// <Summary>
/// Logon account
/// </Summary>
Public string UName {get; set ;}
/// <Summary>
/// Logon Password
/// </Summary>
Public string UPassWord {get; set ;}
On the new page, we need to define two inputs, and then enter the Logon account and password. Note that when defining two inputs, you must set the name attribute of the input to correspond to the object class, of course, our view also needs to define the type as Users (entity Class, Class Name) and set the action and method attributes.
Copy codeThe Code is as follows:
<% @ Page Language = "C #" Inherits = "System. Web. Mvc. ViewPage <Web. Models. Users>" %>
<Form action = "/Demo/Add/" method = "post">
<Table>
<Tr>
<Td> Logon account: </td>
<Td> <input type = "text" name = "UName"/> </td>
</Tr>
<Tr>
<Td> logon password: </td>
<Td> <input type = "password" name = "UPassWord"/> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "center"> <input type = "submit" value = "submit"/> </td>
</Tr>
</Table>
</Form>
Because MVC will automatically map the form to the fields of the object class, it is okay to directly obtain this object class in the controller. Because some shoes obtain data through Request. Form.
Copy codeThe Code is as follows:
[HttpPost]
// Add
Public ActionResult Add (Users model)
{
// Here you can add operations
// Bll. Add (model );
Return RedirectToAction ("List ");
}
In this way, the values in the form are saved in the object class, so you do not need to obtain values one by one. The modified version is similar to this one. I will not go into details here. The DEMO will be downloaded at the end of the article.
Click to download