今天做到这个,记录一下。
解决的办法是。定义一个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: