.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
原创粉丝点击