我有一个视图,它使用一个名为newsModels的模型,它使用IpagedList,但是在同一视图中我添加了一个部分视图,我想列出添加到网站的10个最新消息项,我想要这个部分视图将包含在newsitems视图中以及整个站点的其他视图中。
这样做的最佳方法是什么。我只是成功地列出了IpagedList显示的新网站的标题,所以不太正确。
我假设我需要为局部视图创建一个单独的控制器,但我无法找到有关如何执行此操作的任何指导。
任何人都可以提供任何建议/指导
//Models
public class partialNewsMenu
{
public IEnumerable<NewsModels> newsTitle { get; set; }
}
public class NewsModels
{
public int id { get; set; }
public string newsTitle { get; set; }
[AllowHtml]
public string newsDesc { get; set; }
public string imagePath { get; set; }
public string addedBy { get; set; }
public DateTime dateAdded { get; set; }
public bool publishNewsItem { get; set; }
}
//Controller
public ActionResult OpsiumNews(string sortby, int? page)
{
ViewBag.ImageName = "MSLOffice.jpg";
ViewBag.ImageDesc = "Opsium News";
ViewBag.OverlayText = "N/A";
ViewBag.pageTitle = "Opsium News";
ViewBag.Title = "Opsium | News";
if (sortby == null)
{
ViewBag.MostRecent = "Selected";
}
else
{
ViewBag.MostRecent = "";
}
ViewBag.Oldest = "";
ViewBag.PostedBy = "";
ViewBag.ListNewsModels = new SelectList(db.NewsModels, "Id", "newsTitle");
var NewsModelsList = db.NewsModels.Select(s => s.newsTitle);//.Where(s => s.publishNewsItem);
var NewsModels = db.NewsModels.Where(c => c.publishNewsItem);
switch (sortby)
{
case "1":
NewsModels = NewsModels.OrderBy(s => s.dateAdded);
ViewBag.MostRecent = "Selected";
break;
case "2":
NewsModels = NewsModels.OrderByDescending(s => s.dateAdded);
ViewBag.Oldest = "Selected";
break;
case "3":
NewsModels = NewsModels.OrderBy(s => s.addedBy);
ViewBag.PostedBy = "Selected";
break;
default: // Name ascending
NewsModels = NewsModels.OrderBy(s => s.dateAdded);
ViewBag.MostRecent = "Selected";
break;
}
ViewBag.currentsort = sortby;
int pageSize = 1;
int pageNumber = (page ?? 1);
return View("OpsiumNews", NewsModels.ToPagedList(pageNumber, pageSize));
}
//View
@model PagedList.IPagedList<Opsium_website.Models.NewsModels>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
<div class="Sub-content" id="containcontent">
<div class="rightSideHeader">
<div class="container">
<div class="row">
<div class="col-md-15 addBottomMargin">
@using (Html.BeginForm("OpsiumNews", "UsefulInformation", FormMethod.Get))
{
foreach (var item in Model)
{
<p>
Order by
<select name="sortby" id="sortby" onchange="submit();">
<option value="1" @ViewBag.MostRecent>Most Recent</option>
<option value="2" @ViewBag.Oldest>Oldest</option>
<option value="3" @ViewBag.PostedBy>Posted by</option>
</select>
</p>
<div class="col-md-9 addBottomMargin" id="contentText">
<h3>@Html.DisplayFor(model => item.newsTitle)</h3>
<img src="~/Images/NewsImages/@Html.DisplayFor(model => item.imagePath)" alt="@Html.DisplayFor(model => item.newsTitle)" id="newsImage"/>
@MvcHtmlString.Create(item.newsDesc)
<p><b>Date Added:</b>@String.Format("{0:F}", item.dateAdded) - <b>Added By:</b> @Html.DisplayFor(model => item.addedBy)</p>
</div>
}
}
<div class="col-md-2 addBottomMargin" id="contentText">
@{Html.RenderPartial("_NewsTitlesList");}
</div>
<div class="col-md-9 addBottomMargin">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("OpsiumNews",
new { page, sortby = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
</div>
</div>
</div>
</div>
</div>
//Partial View
@model IEnumerable<Opsium_website.Models.partialNewsMenu>
@using PagedList.Mvc;
@foreach(var items in Model){
@Html.DisplayFor(model=>items.newsTitle)
}
//错误
传递到字典中的模型项的类型为'PagedList.PagedList 1[Opsium_website.Models.NewsModels]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [Opsium_website.Models.partialNewsMenu]'。