.net mvc4 一个 view 显示多个 model

来源:互联网 发布:淘宝在哪改收货地址 编辑:程序博客网 时间:2024/05/14 22:58

今天做到这个,记录一下。

解决的办法是。定义一个class:viewModel。把要显示的model放进去。

Controller:

[csharp] view plain copy
print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcService.Models;  
  7.   
  8. namespace MvcService.Controllers  
  9. {  
  10.     public class viewModel  
  11.     {  
  12.         public List<New> news { getset; }  
  13.         public List<Category> categories { getset; }  
  14.   
  15.         public viewModel(List<New> newsList, List<Category> categoriesList)  
  16.         {  
  17.             this.news = newsList;  
  18.             this.categories = categoriesList;  
  19.         }  
  20.     }  
  21.   
  22.     public class HomeController : Controller  
  23.     {  
  24.         private ServiceEntities db = new ServiceEntities();  
  25.   
  26.         public ActionResult Index()  
  27.         {  
  28.             var vm = new viewModel(db.News.ToList(), db.Categories.ToList());  
  29.   
  30.             vm.news = (from n in db.News  
  31.                        where n.isDel == false && n.state == true  
  32.                        orderby n.createTime descending  
  33.                        select n).Take(16).ToList();  
  34.   
  35.             vm.categories = (from n in db.Categories  
  36.                              where n.pId == 0 && n.isDel == false && n.state == true  
  37.                              select n).ToList();  
  38.   
  39.             return View(vm);  
  40.         }  
  41.     }  
View:
[html] view plain copy
print?
  1. @using MvcService.Models  
  2. @model MvcService.Controllers.viewModel //引用HomeControllers中自定义的viewModel  
  3. @{  
  4.     ViewBag.Title = "Index";  
  5.     Layout = "~/Views/Shared/_IndexLayout.cshtml";  
  6. }  
  7. <div class="main_1">  
  8.     <h2>热点问题</h2>  
  9.     <ul>  
  10.         @if (Model != null)  
  11.         {  
  12.             foreach (var n in Model.news)  
  13.             {  
  14.             <li><a href="/home/newsdetails">@(n.title.Length > 25 ? n.title.Substring(0, 25) + "..." : n.title)</a> </li>  
  15.             }  
  16.         }  
  17.     </ul>  
  18.     <div class="clear"></div>  
  19. </div>  
  20. <!--main_1结束-->  
  21.   
  22. <div class="main_2">  
  23.     @if (Model != null)  
  24.     {  
  25.         foreach (var c in Model.categories)  
  26.         {  
  27.         <dl>  
  28.             <dt>@c.name</dt>  
  29.             <dd>  
  30.                 <a href="/home/newslist">会员信息</a>   
  31.                 <a href="/home/newslist">注册及登录</a>  
  32.             </dd>  
  33.         </dl>  
  34.         }  
  35.     }  
  36. </div>  
  37. <!--main_2结束-->  
附上效果图.


还有一个问题,我想橙色的显示该类别的子类别。要怎么写。。大神指导下。。

我给出category的表结构。  pId = 0 代表的是父级菜单。



*************************************************12月24更新************************************************************

解决上面的问题,不过不用上面 viewModel 的办法了。
我们用ViewBag。在此之前,先新建一个class:Cate.cs,用来存放取出来的数据。
Cate.cs:

[csharp] view plain copy
print?
  1. namespace MvcService.Models  
  2. {  
  3.     public class Cate  
  4.     {  
  5.         public string name { getset; }  //1级 name  
  6.         public int[] id { getset; }     //2级 id  
  7.         public string[] cname { getset; }  //2级 name  
  8.     }  
  9. }  
Controller:
[csharp] view plain copy
print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcService.Models;  
  7.   
  8. namespace MvcServiceCenter.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         private ServiceEntities db = new ServiceEntities();  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             //热点  
  17.             ViewBag.redian = (from n in db.News where n.isDel == false && n.state == true  
  18.                               orderby n.aClick descending select n).Take(16).ToList();  
  19.   
  20.             //栏目  
  21.             //ViewBag.lanmu = (from c in db.Categories where c.pId == 0 && c.isDel == false && c.state == true select c).ToList();  
  22.             List<Cate> list = new List<Cate>();  
  23.             var one = db.Categories.Where(a => a.pId == 0);    //取出第一级.  
  24.             foreach (var b in one)  
  25.             {  
  26.                 Cate cate = new Cate();  
  27.                 cate.name = b.name;  //保存第一级的name  
  28.                 var two = db.Categories.Where(c => c.pId == b.id);  //取出第二级  
  29.                 int[] id = new int[two.Count()];  
  30.                 string[] ss = new string[two.Count()];  
  31.                 int num = 0;  
  32.                 foreach (var d in two)  
  33.                 {  
  34.                     ss[num] = d.name;  
  35.                     id[num] = d.id;  
  36.                     num++;  
  37.                 }  
  38.                 cate.id = id;  //保存第二级的id  
  39.                 cate.cname = ss;  //保存第二级的name  
  40.                 list.Add(cate);    
  41.             }  
  42.             ViewBag.lanmu = list;  
  43.   
  44.             return View();  
  45.         }  
  46.     }  
  47. }  
View:
[html] view plain copy
print?
  1. @{  
  2.     ViewBag.Title = "首页";  
  3.     Layout = "~/Views/Shared/_IndexLayout.cshtml";  
  4. }  
  5. <div class="main_1">  
  6.     <h2>热点问题</h2>  
  7.     <ul>  
  8.         @if (ViewBag.redian != null)  
  9.         {  
  10.             foreach (var n in ViewBag.redian)  
  11.             {  
  12.             <li><a href="/home/newsdetails/@n.id" title="@n.title">  
  13.                 @(n.title.Length > 25 ? n.title.Substring(0, 25) + "..." : n.title)</a></li>  
  14.             }  
  15.         }  
  16.     </ul>  
  17.     <div class="clear"></div>  
  18. </div>  
  19. <!--main_1结束-->  
  20. <div class="main_2">  
  21.     @if (ViewBag.lanmu != null)  
  22.     {  
  23.         foreach (var c in ViewBag.lanmu)  
  24.         {  
  25.         <dl>  
  26.             <dt>@c.name</dt>  
  27.             <dd>  
  28.                 @for (int i = 0; i < c.cname.Length; i++)  
  29.                 {  
  30.                     <a href="/home/newslist/@c.id[i]">@c.cname[i]</a>  
  31.                 }  
  32.             </dd>  
  33.         </dl>  
  34.         }  
  35.     }  
  36. </div>  
  37. <!--main_2结束-->  

图: