Asp.net MVC uses pagedlist.mvc to achieve paging effect

Time:2021-6-4

In the last articleDB first of EFThere are two problems in this paper

1. The add / edit page displays the attribute name, not the custom name (such as name, major…)

2. Validation is not added when adding / editing

3. Pagination is used for data display

@HTML. Labelfor (model = > model. Name, htmlattributes: new {@ class = > control label col-md-2 “}) is the” label “of the display attribute name. If no display attribute is specified, the attribute name will be displayed directly

Generally, the entity model files and codes generated by the general database are not modified directly (to prevent the next generation from being covered). In this case, verification and entity separation should be used

Add a validation class. The code is as follows:

using System.ComponentModel.DataAnnotations;

namespace Zhong.Web.Models
{
 [MetadataType(typeof(T_StudentValidateInfo))]
 public partial class T_Student
 {
 }
 public class T_StudentValidateInfo
 {
  [display (name = "name")]
  [required (ErrorMessage = name cannot be empty)]
  [stringlength (10, ErrorMessage = name length exceeds limit)]
  public string Name { get; set; }

  [display (name = student number)]
  [Required]
  [stringlength (20, minimumlength = 10, ErrorMessage = 10-20 ")]
  public string StudentId { get; set; }
 }
}

At this time, the front desk visits and submits:

It can be found from the above figure that the name has changed to “name”, the studentsid has changed to “student number”. After clicking the Create button, a verification prompt message appears.

Pagedlist. MVC plug-in is used for paging real-time, and nuget can be used to add references

Add a list controller method to studentscontroller:


public ActionResult List(int page = 1)
{
 //var students = entities.T_Student.OrderBy(s => s.Id).Skip((page - 1) * 2).Take(2);
 var students = entities.T_Student.OrderBy(s => s.Id);
 return View(students.ToPagedList(page, 2));
}

The view code is as follows:

@using PagedList.Mvc
@model PagedList.IPagedList<Zhong.Web.Models.T_Student>

@{
 ViewBag.Title = "List";
}

<h2>List</h2>

<p>
 @Html.ActionLink("Create New", "Create")
</p>
<table>
 <tr>
  <th>
   full name
  </th>
  <th>
   Student number
  </th>
  <th>
   major
  </th>
  <th></th>
 </tr>

@foreach (var item in Model) {
 <tr>
  <td>
   @Html.DisplayFor(modelItem => item.Name)
  </td>
  <td>
   @Html.DisplayFor(modelItem => item.StudentId)
  </td>
  <td>
   @Html.DisplayFor(modelItem => item.T_Major.Name)
  </td>
  <td>
   @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
   @Html.ActionLink("Details", "Details", new { id=item.Id }) |
   @Html.ActionLink("Delete", "Delete", new { id=item.Id })
  </td>
 </tr>
}

</table>
@Html.PagedListPager(Model,page => Url.Action("List",new { page}))

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.