.net mvc4 一个 view 显示多个 model

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

解决的办法是。定义一个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 news { get; set; }
        public List categories { get; set; }

        public viewModel(List newsList, List 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";
}

热点问题

@if (Model != null) { foreach (var c in Model.categories) {
@c.name
会员信息 注册及登录
} }
附上效果图.

.net mvc4 一个 view 显示多个 model_第1张图片
还有一个问题,我想橙色的显示该类别的子类别。要怎么写。。大神指导下。。

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

.net mvc4 一个 view 显示多个 model_第2张图片.net mvc4 一个 view 显示多个 model_第3张图片


*************************************************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 list = new List();
            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";
}

热点问题

@if (ViewBag.lanmu != null) { foreach (var c in ViewBag.lanmu) {
@c.name
@for (int i = 0; i < c.cname.Length; i++) { @c.cname[i] }
} }

图:
.net mvc4 一个 view 显示多个 model_第4张图片

你可能感兴趣的:(.net,MVC)