.net mvc4 一个 view 显示多个 model
来源:互联网 发布:淘宝在哪改收货地址 编辑:程序博客网 时间:2024/05/14 22:58
今天做到这个,记录一下。
解决的办法是。定义一个class:viewModel。把要显示的model放进去。
Controller:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcService.Models;
- namespace MvcService.Controllers
- {
- public class viewModel
- {
- public List<New> news { get; set; }
- public List<Category> categories { get; set; }
- public viewModel(List<New> newsList, List<Category> categoriesList)
- {
- this.news = newsList;
- this.categories = categoriesList;
- }
- }
- public class HomeController : Controller
- {
- private ServiceEntities db = new ServiceEntities();
- public ActionResult Index()
- {
- var vm = new viewModel(db.News.ToList(), db.Categories.ToList());
- vm.news = (from n in db.News
- where n.isDel == false && n.state == true
- orderby n.createTime descending
- select n).Take(16).ToList();
- vm.categories = (from n in db.Categories
- where n.pId == 0 && n.isDel == false && n.state == true
- select n).ToList();
- return View(vm);
- }
- }
- @using MvcService.Models
- @model MvcService.Controllers.viewModel //引用HomeControllers中自定义的viewModel
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_IndexLayout.cshtml";
- }
- <div class="main_1">
- <h2>热点问题</h2>
- <ul>
- @if (Model != null)
- {
- foreach (var n in Model.news)
- {
- <li><a href="/home/newsdetails">@(n.title.Length > 25 ? n.title.Substring(0, 25) + "..." : n.title)</a> </li>
- }
- }
- </ul>
- <div class="clear"></div>
- </div>
- <!--main_1结束-->
- <div class="main_2">
- @if (Model != null)
- {
- foreach (var c in Model.categories)
- {
- <dl>
- <dt>@c.name</dt>
- <dd>
- <a href="/home/newslist">会员信息</a>
- <a href="/home/newslist">注册及登录</a>
- </dd>
- </dl>
- }
- }
- </div>
- <!--main_2结束-->
还有一个问题,我想橙色的显示该类别的子类别。要怎么写。。大神指导下。。
我给出category的表结构。 pId = 0 代表的是父级菜单。
*************************************************12月24更新************************************************************
解决上面的问题,不过不用上面 viewModel 的办法了。
我们用ViewBag。在此之前,先新建一个class:Cate.cs,用来存放取出来的数据。
Cate.cs:
- namespace MvcService.Models
- {
- public class Cate
- {
- public string name { get; set; } //1级 name
- public int[] id { get; set; } //2级 id
- public string[] cname { get; set; } //2级 name
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcService.Models;
- namespace MvcServiceCenter.Controllers
- {
- public class HomeController : Controller
- {
- private ServiceEntities db = new ServiceEntities();
- public ActionResult Index()
- {
- //热点
- ViewBag.redian = (from n in db.News where n.isDel == false && n.state == true
- orderby n.aClick descending select n).Take(16).ToList();
- //栏目
- //ViewBag.lanmu = (from c in db.Categories where c.pId == 0 && c.isDel == false && c.state == true select c).ToList();
- List<Cate> list = new List<Cate>();
- var one = db.Categories.Where(a => a.pId == 0); //取出第一级.
- foreach (var b in one)
- {
- Cate cate = new Cate();
- cate.name = b.name; //保存第一级的name
- var two = db.Categories.Where(c => c.pId == b.id); //取出第二级
- int[] id = new int[two.Count()];
- string[] ss = new string[two.Count()];
- int num = 0;
- foreach (var d in two)
- {
- ss[num] = d.name;
- id[num] = d.id;
- num++;
- }
- cate.id = id; //保存第二级的id
- cate.cname = ss; //保存第二级的name
- list.Add(cate);
- }
- ViewBag.lanmu = list;
- return View();
- }
- }
- }
- @{
- ViewBag.Title = "首页";
- Layout = "~/Views/Shared/_IndexLayout.cshtml";
- }
- <div class="main_1">
- <h2>热点问题</h2>
- <ul>
- @if (ViewBag.redian != null)
- {
- foreach (var n in ViewBag.redian)
- {
- <li><a href="/home/newsdetails/@n.id" title="@n.title">
- @(n.title.Length > 25 ? n.title.Substring(0, 25) + "..." : n.title)</a></li>
- }
- }
- </ul>
- <div class="clear"></div>
- </div>
- <!--main_1结束-->
- <div class="main_2">
- @if (ViewBag.lanmu != null)
- {
- foreach (var c in ViewBag.lanmu)
- {
- <dl>
- <dt>@c.name</dt>
- <dd>
- @for (int i = 0; i < c.cname.Length; i++)
- {
- <a href="/home/newslist/@c.id[i]">@c.cname[i]</a>
- }
- </dd>
- </dl>
- }
- }
- </div>
- <!--main_2结束-->
图:
阅读全文
0 0
- .net mvc4 一个 view 显示多个 model
- .net mvc4 一个 view 显示多个 model
- 一个view显示多个model(一个页面显示多张表)
- ASP.NET MVC4中@model使用多个类型实例的方法
- ASP.NET MVC4中@model使用多个类型实例的方法
- .net mvc4 model 的配置
- 【翻译】ASP.NET MVC4 入门(三)添加一个View
- ASP.NET MVC2 同一view返回多个Model或数据集的方法
- ASP.NET MVC3.0中同一View如何返回多个Model或数据集
- android_fragment一个屏幕显示多个view.xml
- MVC4 Model View Controller分离成独立项目
- Asp.Net MVC4 系列--进阶篇之Model(1)
- Asp.Net MVC4 系列--进阶篇之Model(2)
- ASP.NET-MVC4--Layout母版页面传入数据Model模型
- AspNet MVC4 教学-4:Asp.Net MVC4 利用ViewData.Model传递数据
- 自定义BannerView,显示下个一个view部分界面
- ASP.NET MVC 初学笔记.2 一个View多个Models的对应
- qt model 与 view 的数据显示
- Linux下的tar压缩解压缩命令详解
- 23种设计模式(22):组合模式
- Java I/O
- WPF DataGrid Drag
- html5手机开发cookie的用法总结
- .net mvc4 一个 view 显示多个 model
- 大数据应用统一集成平台CDAP简介
- HDU1205
- 二进制的转换
- 如何判断IP地址是否合法
- 尴尬了!OLED屏幕还未普及开来,人们就开始讨论起Micro-LED了!
- Linux安装Tomcat
- 【网易 MC】音频处理与压缩技术
- (转)【WebView的cookie机制 】轻松搞定WebView cookie同步问题