I'm pretty new to MVC. I'm trying to create a page where a news story is pulled from a SQL Server database to populate the main area of the page, while I have a sidebar where all the headlines are listed. I've tried using a partial view for the sidebar.
Here's the code from the controller which populates the views:
public ActionResult _ArticleList()
{
return View(db.Articles.OrderByDescending(a => a.id).ToList());
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Articles articles = db.Articles.Find(id);
if (articles == null)
{
return HttpNotFound();
}
return View(articles);
My main view - Details.cshtml
@model MyApplication.Models.Articles
<div id="container">
{
<div id="main" class="col-md-9">
<h2> @Html.DisplayFor(model => model.Headline)</h2>
<p style="white-space:pre-line">
@Html.DisplayFor(model => model.Content)
</p>
</div>
<div id="rightnav" class="col-md-3">
@Html.Partial("_ArticleList")
</div>
</div>
and my partial view - _ArticleList.cshtml
@model IEnumerable<MyApplication.Models.Articles>
@foreach (var item in Model)
{
<a href="/Details/@item.id"> @Html.DisplayFor(modelItem => item.Headline)</a>
<br>
}
I'm getting a conflict between @model MyApplication.Models.Articles
in the main view and @model IEnumerable<MyApplication.Models.Articles>
in the partial.
Can anyone suggest a way around this?