MVC ---- IEnumerable<T>、List<T> 前台遍历展示
突然做前台数据展示,发现IEnumerable 对一个列表的展示还是可以,但要是多个类型放在一个表中如何处理呢,如下:
一个类IEnumerable遍历
后台
public IEnumerable<NewsEntity> GetNotice() { int type = 1; return noticeBLL.GetListByType(type); }
前台:
引用
@model IEnumerable<LeaRun.Application.Entity.PublicInfoManage.NewsEntity>
<div class="panel-body"> <ul> @foreach (var item in Model) { <li><a href="#">@item.FullHead</a><span class="time">@item.CreateDate.ToString().Split(' ')[0].Replace("/", "-")</span></li> } </ul> </div>
多个类型放在同一个表中处理方式
首先在control中建一个临时类
/// <summary> /// 公告实体封装 /// </summary> public class GetIEnumerable { /// <summary> /// 公告 /// </summary> public IEnumerable<NewsEntity> Notice { get; set; } /// <summary> /// 新闻 /// </summary> public IEnumerable<NewsEntity> News { get; set; } }
用法:
public GetIEnumerable GetNotice() { GetIEnumerable ge = new GetIEnumerable(); int type = 2;//2、公告 ge.Notice = noticeBLL.GetListByType(type); type = 1; //1、新闻 ge.News = noticeBLL.GetListByType(type); return ge; } //网页面上跳转 public ActionResult AdminLTEDesktop() { return View(GetNotice()); }
页面:
引用
@model LeaRun.Application.Web.Controllers.GetIEnumerable
<div class="panel-body"> <ul> @foreach (var item in Model.News) { <li><a href="#">@item.FullHead</a><span class="time">@item.CreateDate.ToString().Split(' ')[0].Replace("/","-")</span></li> } </ul> </div> <div class="panel-body"> <ul> @foreach (var item in Model.Notice) { <li><a href="#">@item.FullHead</a><span class="time">@item.CreateDate.ToString().Split(' ')[0].Replace("/", "-")</span></li> } </ul> </div>
======================================================================================================
List<T> 页面遍历
引用命名空间
@using System.Data;
@using NFine.Domain._03_Entity.POCO.Business;
@model List<NFine.Domain._03_Entity.POCO.Business.CustomerAndFollow>
@{ foreach (CustomerAndFollow activities in Model) { <table class="ui-jqgrid-btable ui-common-table table table-bordered" style=" background:#ffffff!important; margin-bottom:30px;"> <tbody> <tr class="jqgfirstrow"> <td style="height:50px;line-height:50px;">@activities.CustomerId</td> <td style="height:50px;line-height:50px;">@activities.F_FullName</td> <td style="height:50px;line-height:50px;">@activities.F_Msisdn</td> <td style="height:50px;line-height:50px;">@activities.F_CreatorTime</td> <td style="height:50px;line-height:50px;">@activities.F_CreatorUserName</td> </tr> <tr> <td colspan="5"> <textarea id="@activities.FollowId" class="ckeditor">@activities.F_Description</textarea> </td> </tr> </tbody> </table> }}
后台:
public override ActionResult Index() { int record = 0; Pagination page = new Pagination(); page.rows = 1; page.page = 2; page.records = record; List<CustomerAndFollow> follow = custapp.GetFollowList(page, out record); return View(follow); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通