Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write an ASP.NET MVC5 website to display a list of articles

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is "how to write an ASP.NET MVC5 website development display article list", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn how to write ASP.NET MVC5 website development display article list!

Last effect picture:

1. In IBLL

Add a method to get a list of common models in the InterfaceCommonModelService interface

First of all, sorting method

/ sort / data entity set / sort code [default: ID descending] / IQueryable Order (IQueryable entitys, int roderCode) Query data method / query paged data list / Total records / Model [All all] / / Page number / Records per page / / title [do not set empty string] / column ID [do not set 0] / user name [do not set empty string] / / / start date [may be null] / / due date [may be null] / sorting code / paging data list IQueryable FindPageList (out int totalRecord Int pageIndex, int pageSize, string model, string title, int categoryID, string inputer, Nullable fromDate, Nullable toDate, int orderCode)

2 、 BLL

Write the code for the implementation of the method in CommonModelService, the content is very simple, mainly ideas, directly on the code public IQueryable FindPageList (out int totalRecord, int pageIndex, int pageSize, string model, string title, int categoryID, string inputer, Nullable fromDate, Nullable toDate, int orderCode) {/ / get the entity list IQueryable _ commonModels = CurrentRepository.Entities; if (model = = null | | model! = "All") _ commonModels = _ commonModels.Where (cm = > cm.Model = = model) If (! string.IsNullOrEmpty (title)) _ commonModels = _ commonModels.Where (cm = > cm.Title.Contains (title)); if (categoryID > 0) _ commonModels = _ commonModels.Where (cm = > cm.CategoryID = = categoryID); if (! string.IsNullOrEmpty (inputer)) _ commonModels = _ commonModels.Where (cm = > cm.Inputer = = inputer); if (fromDate! = null) _ commonModels = _ commonModels.Where (cm = > cm.ReleaseDate > = fromDate) If (toDate! = null) _ commonModels = _ commonModels.Where (cm = > cm.ReleaseDate cm.ReleaseDate); break;} return entitys;}

3 、 web

Since CommonModel is not consistent with the data displayed in our foreground, a view model CommonModelViewModel is constructed in Ninesky.Web.Models in order to take care of the data display in datagrid.

Using System;namespace Ninesky.Web.Models {/ CommonModel View Model / create: 2014.03.10 / public class CommonModelViewModel {public int ModelID {get; set;} / column ID / public int CategoryID {get; set;} / column name / public string CategoryName {get; set } / public string Model {get; set;} / title / public string Title {get; set;} / public string Inputer {get; set;} / click / public int Hits {get; set } / release date / public DateTime ReleaseDate {get; set;} / status / / public int Status {get; set;} / status text / public string StatusString {get {return Ninesky.Models.CommonModel.StatusList [Status];}} / homepage image / public string DefaultPicUrl {get; set;}

Add a JsonList method to ArticleController that returns the type json

/ list of articles Json [pay attention to permission issues, can ordinary people access it? ] / title / / enter / / column / from / / date to date / / Page number / record per page / public ActionResult JsonList (string title, string input, Nullable category, Nullable fromDate, Nullable toDate, int pageIndex = 1, int pageSize = 20) {if (category = = null) category = 0; int _ total Var _ rows = commonModelService.FindPageList (out _ total, pageIndex, pageSize, "Article", title, (int) category, input, fromDate, toDate, 0). Select (cm = > new Ninesky.Web.Models.CommonModelViewModel () {CategoryID = cm.CategoryID, CategoryName = cm.Category.Name, DefaultPicUrl = cm.DefaultPicUrl, Hits = cm.Hits, Inputer = cm.Inputer, Model = cm.Model, ModelID = cm.ModelID, ReleaseDate = cm.ReleaseDate Status = cm.Status, Title = cm.Title}) Return Json (new {total = _ total, rows = _ rows.ToList ());}

The following is the interface. In adding the List method, no data is provided here. The data is obtained in JsonList.

/ / all articles / public ActionResult List () {return View ();}

Right-click to add view

Modify refresh column title entry date-query $("# article_list") .datagrid ({loadMsg: 'loading.' , pagination:true, url:'@ Url.Action ("JsonList", "Article")', columns: [{field: 'ModelID', title:' ID', checkbox: true}, {field: 'CategoryName', title:' column'}, {field: 'Title', title:' title'}, {field: 'Inputer', title:' input', align: 'right'}, {field:' Hits' Title: 'click', align: 'right'}, {field:' ReleaseDate', title: 'release date', align: 'right', formatter: function (value, row, index) {return jsonDateFormat (value) }}, {field: 'StatusString', title:' status', width: 100, align: 'right'}]], toolbar:' # toolbar', idField: 'ModelID',}) / find $("# btn_search") .click (function () {$("# article_list"). Datagrid ('load', {title: $("# textbox_title"). Val (), input: $("# textbox_inputer"). Val (), category: $("# combo_category"). Combotree (' getValue'), fromDate: $("# datebox_fromdate"). Datebox ('getValue') ToDate: $("# datebox_todate") .datebox ('getValue')}) });} at this point, I believe you have a deeper understanding of "how to write an ASP.NET MVC5 website development display article list". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report