不在局部视图中显示 take(4) 数据
not show take(4) Data in Partial View
i 在 ASP.Net MVC 中创建新闻站点。
我想在数据库中显示 4 个最后一行数据,当它们的类型是 ("SP") 时。
但是当我 运行 Project 只显示最后一行时。
如何解决?
我的数据库:Database Record
我创建存储库这个 .
public List<Tbl_News> GetSpecialNews()
{
try
{
List<Tbl_News> qGetSpecialNews = (from a in db.Tbl_News
where a.Type.Equals("sp")
select a).OrderByDescending(s => s.ID).Skip(0).Take(4).ToList();
return qGetSpecialNews;
}
catch (Exception)
{
return null;
}
}
局部视图:
@{
foreach (var item in RNews.GetSpecialNews())
{
<div class="sec-special-page-news-img">
<img src="~/Content/img/NewsPic/@item.Image" />
</div>
<div class="sec-special-page-news-body">
<a href="@item.ID">@item.Title</a>
<ul class="mini-info">
<li>@item.Date<span class="glyphicon glyphicon-time"></span></li>
</ul>
</div>
}
}
家庭控制:
public class HomeController : Controller
{
Rep_News RNews = new Rep_News();
// GET: Home
public ActionResult Index()
{
var q = RNews.GetSpecialNews();
return View(q);
}
}
引用评论
Your code should return 4 records. Also, you do not need to call
RNews.GetSpecialNews() method again in your view as your are doing
that in your action method and passing it to the view. Just do a
foreach on the Model
public List<Tbl_News> GetSpecialNews() {
try {
List<Tbl_News> qGetSpecialNews =
(from a in db.Tbl_News
where a.Type.Equals("sp")
select a)
.OrderByDescending(s => s.ID)
.Take(4)
.ToList();
return qGetSpecialNews;
}
catch (Exception) {
return null;
}
}
查看
@model List<Tbl_News>
@{
foreach (var item in Model)
{
<div class="sec-special-page-news-img">
<img src="~/Content/img/NewsPic/@item.Image" />
</div>
<div class="sec-special-page-news-body">
<a href="@item.ID">@item.Title</a>
<ul class="mini-info">
<li>@item.Date<span class="glyphicon glyphicon-time"></span></li>
</ul>
</div>
}
}
i 在 ASP.Net MVC 中创建新闻站点。
我想在数据库中显示 4 个最后一行数据,当它们的类型是 ("SP") 时。
但是当我 运行 Project 只显示最后一行时。
如何解决?
我的数据库:Database Record
我创建存储库这个 .
public List<Tbl_News> GetSpecialNews()
{
try
{
List<Tbl_News> qGetSpecialNews = (from a in db.Tbl_News
where a.Type.Equals("sp")
select a).OrderByDescending(s => s.ID).Skip(0).Take(4).ToList();
return qGetSpecialNews;
}
catch (Exception)
{
return null;
}
}
局部视图:
@{
foreach (var item in RNews.GetSpecialNews())
{
<div class="sec-special-page-news-img">
<img src="~/Content/img/NewsPic/@item.Image" />
</div>
<div class="sec-special-page-news-body">
<a href="@item.ID">@item.Title</a>
<ul class="mini-info">
<li>@item.Date<span class="glyphicon glyphicon-time"></span></li>
</ul>
</div>
}
}
家庭控制:
public class HomeController : Controller
{
Rep_News RNews = new Rep_News();
// GET: Home
public ActionResult Index()
{
var q = RNews.GetSpecialNews();
return View(q);
}
}
引用评论
Your code should return 4 records. Also, you do not need to call RNews.GetSpecialNews() method again in your view as your are doing that in your action method and passing it to the view. Just do a foreach on the Model
public List<Tbl_News> GetSpecialNews() {
try {
List<Tbl_News> qGetSpecialNews =
(from a in db.Tbl_News
where a.Type.Equals("sp")
select a)
.OrderByDescending(s => s.ID)
.Take(4)
.ToList();
return qGetSpecialNews;
}
catch (Exception) {
return null;
}
}
查看
@model List<Tbl_News>
@{
foreach (var item in Model)
{
<div class="sec-special-page-news-img">
<img src="~/Content/img/NewsPic/@item.Image" />
</div>
<div class="sec-special-page-news-body">
<a href="@item.ID">@item.Title</a>
<ul class="mini-info">
<li>@item.Date<span class="glyphicon glyphicon-time"></span></li>
</ul>
</div>
}
}