传入字典的模型项的类型为“System.Collections.Generic.List`1[MvcApplication1.Models.News]”,但此字典需要“MvcApplication1.Models.News”类型的模型项。
//my Controller
public class HomeController : Controller
{
CouncilDb _db=new CouncilDb() ;
public ActionResult Index()
{
var News = _db.News
.Take(10);
var Banner = (from r in _db.Banner orderby r.id descending select r).FirstOrDefault();
maz model = new maz();
model.Banner = Banner;
model.News = News.ToList();
return View(model);
}
protected override void Dispose(bool disposing)
{
if (_db != null)
{
_db.Dispose();
}
base.Dispose(disposing);
}
}
public class maz
{
public List<News> News { get; set; }
public Banner Banner { get; set; }
}
//索引视图
@model MvcApplication1.Controllers.maz
@{
ViewBag.Title = "Home Page";
}
<!-- Banner -->
<!-- Banner -->
<div id="banner">
<h2> @Model.Banner.H2</h2>
<span class="byline"> @Model.Banner.Span </span>
</div>
@Html.Partial("_News",Model.News )
//部分视图
@model MvcApplication1.Models.News
<!-- Carousel -->
<div class="carousel">
<div class="reel">
<article>
<a class="image featured">
<img src="@Model.ImgUrl " alt="" /></a>
<header>
<h3>@Html.ActionLink(@Model.Title , "serch", "Home")</h3>
</header>
<p>@Model.Body </p>
</article>
</div>
</div>