.net mvc4 一个 view 显示多个 model
来源:互联网 发布:mac无法显示移动硬盘 编辑:程序博客网 时间:2024/05/29 07:35
今天做到这个,记录一下。
解决的办法是。定义一个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); } }View:
@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 }}Controller:
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(); } }}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结束-->
图:
1 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 的数据显示
- UIView 转化为 UIImage,将UIImage 保存到沙盒中
- JavaScript学习第四课--语句
- 当你想放弃时,一定要看看这个漫画!
- vim+ctags+cscope 用法
- Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
- .net mvc4 一个 view 显示多个 model
- 捕获程序中未捕获的异常
- 类的静态成员函数带来了什么好处,应该在什么时候使用?
- java中的匿名内部类总结
- 设置Dialog
- hdu 1379 DNA Sorting
- 文件关联后vb.net 软件如何响应被关联文件的双击事件
- 环形缓冲区的实现原理
- android 多个edittext的输入和删除