這篇文章主要介紹了asp.net MVC如何使用PagedList.MVC實(shí)現(xiàn)分頁(yè)效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),都江堰企業(yè)網(wǎng)站建設(shè),都江堰品牌網(wǎng)站建設(shè),網(wǎng)站定制,都江堰網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,都江堰網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。在EF之DB First中,存在以下的兩個(gè)問題:
1. 添加/編輯頁(yè)面顯示的是屬性名稱,而非自定義的名稱(如:姓名、專業(yè)...)
2. 添加/編輯時(shí)沒有加入驗(yàn)證
3. 數(shù)據(jù)展示使用分頁(yè)
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 是顯示屬性Name的“標(biāo)簽”,如果沒有指定Display特性,則直接顯示屬性名Name
通用數(shù)據(jù)庫(kù)生成的實(shí)體模型文件與代碼一般不直接修改(防止下次生成時(shí)覆蓋),這里要使用驗(yàn)證與實(shí)體分離
添加一個(gè)驗(yàn)證類,代碼如下 :
using System.ComponentModel.DataAnnotations; namespace Zhong.Web.Models { [MetadataType(typeof(T_StudentValidateInfo))] public partial class T_Student { } public class T_StudentValidateInfo { [Display(Name="姓名")] [Required(ErrorMessage ="姓名不能為空")] [StringLength(10,ErrorMessage ="姓名長(zhǎng)度超出限制")] public string Name { get; set; } [Display(Name="學(xué)號(hào)")] [Required] [StringLength(20,MinimumLength =10,ErrorMessage ="長(zhǎng)度為10-20")] public string StudentId { get; set; } } }
此時(shí)前臺(tái)訪問并提交:
從上圖可以發(fā)現(xiàn)Name變成了“姓名”,StudentsId變成了“學(xué)號(hào)”,點(diǎn)擊Create按鈕后,出現(xiàn)了驗(yàn)證提示信息。
分頁(yè)的實(shí)時(shí)使用PagedList.MVC插件,可以nuget添加引用
StudentsController中增加一個(gè)List的控制器方法:
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)); }
視圖代碼如下:
@using PagedList.Mvc @model PagedList.IPagedList<Zhong.Web.Models.T_Student> @{ ViewBag.Title = "List"; } <h3>List</h3> <p> @Html.ActionLink("Create New", "Create") </p> <table class="table"> <tr> <th> 姓名 </th> <th> 學(xué)號(hào) </th> <th> 專業(yè) </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}))
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“asp.net MVC如何使用PagedList.MVC實(shí)現(xiàn)分頁(yè)效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
當(dāng)前題目:asp.netMVC如何使用PagedList.MVC實(shí)現(xiàn)分頁(yè)效果-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://www.rwnh.cn/article4/ceghoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站制作、服務(wù)器托管、企業(yè)建站、靜態(tài)網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容