多谢版主!搞定了~把代码分享下,让和我一样的菜鸟可以节约一些时间
ZhuYecontrollers代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QunZhongLuXian.Models;//项目名称,引用自定义的Model
namespace QunZhongLuXian.Controllers
{
public class viewModel
{
public List<ZuZhiJiGou> zzjgModel { get; set; }
public List<LingDaoJiangHua> ldjhModel { get; set; }
public List<GongZuoDongTai> gzdtModel { get; set; }
public List<WenJianTongZhi> wjtzModel { get; set; }
public List<XueXiJiaoLiu> xxjlModel { get; set; }
public viewModel(List<Models.ZuZhiJiGou> zzjgList, List<Models.LingDaoJiangHua> ldjhList, List<Models.GongZuoDongTai> gzdtList, List<Models.WenJianTongZhi> wjtjList, List<Models.XueXiJiaoLiu> xxjlList)
{
this.zzjgModel = zzjgList;
this.ldjhModel = ldjhList;
this.gzdtModel = gzdtList;
this.wjtzModel = wjtjList;
this.xxjlModel = xxjlList;
}
}
public class ZhuYeController : Controller
{
QunZhongLuXianContext db = new QunZhongLuXianContext();
public ActionResult Index()
{
var vm = new viewModel(db.ZuZhiJiGous.ToList(), db.LingDaoJiangHuas.ToList(), db.GongZuoDongTais.ToList(), db.WenJianTongZhis.ToList(), db.XueXiJiaoLius.ToList());
vm.zzjgModel = db.ZuZhiJiGous.ToList();
vm.ldjhModel = db.LingDaoJiangHuas.OrderByDescending(p => p.ShiJian).ToList();
vm.gzdtModel = db.GongZuoDongTais.OrderByDescending(p => p.ShiJian).ToList();
vm.wjtzModel = db.WenJianTongZhis.OrderByDescending(p => p.ShiJian).ToList();
vm.xxjlModel = db.XueXiJiaoLius.OrderByDescending(p => p.ShiJian).ToList();
return View(vm);
}
}
}
对应/Views/ZhuYe/Index.cshtml视图代码(部分)
@using QunZhongLuXian.Models
@model QunZhongLuXian.Controllers.viewModel//引用ZhuYeControllers中自定义的viewModel类类型
中间省略,下面是通过Foreach循环显示两个Model的列表数据(分别是Model.ldjhModel和Model.gzdtModel)
<div class="box">
<h1>
<span><a href="@Url.Action("Index", "LingDaoJiangHua")">more</a></span>领导讲话
</h1>
<ul>
@foreach (var item in Model.ldjhModel)
{
<li>
<span class="datetime">@item.ShiJian</span>
<a href="@item.LuJing">@item.BiaoTi</a>
</li>
}
</ul>
</div>
<div class="box">
<h1>
<span><a href="@Url.Action("Index", "GongZuoDongTai")">more</a></span>工作动态
</h1>
<ul>
@foreach (var item in Model.gzdtModel)
{
<li>
<span class="datetime">@item.ShiJian</span>
<a href="@item.LuJing">@item.BiaoTi</a>
</li>
}
</ul>
</div>