【发布时间】:2016-05-15 23:16:01
【问题描述】:
我想知道如何从关系表中获取数据。
我想从名为“Noticias1”的表中获取与“Noticias”相关的图像(Noticias 是一个西班牙语单词,意思是新闻对不起,但它适用于我的大学)。
这里是图表图像
这是我的“Noticias1”表,它获取将包含表“Noticias”中新闻的图像
这是我的“Noticia”表,其中仅包含 1 个“Noticia”,表示英文新闻
如您所见,它仅显示“Noticias”表,其中只有 1 个不是问题的新闻。
现在我想从“Noticias1”获取所有图片到每个新闻 “Noticias”表在我的视图中显示。 (命名的 1_0 将是 精选图片)。
这是我的控制器
public class NoticiasController : Controller
{
// GET: Noticias
public ActionResult Index(int? page)
{
var entities = new Model.CobecaIntranetEntities();
//where n.FeHasta < DateTime.Now && n.Activo
var noticias = from n in entities.Noticias
where n.Activo && n.FeDesde <= DateTime.Now && DateTime.Now <= n.FeHasta
select n;
var noticiasArray = noticias.ToArray();
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(noticiasArray.ToPagedList(pageNumber, pageSize));
}
}
这是我的观点
@model PagedList.IPagedList<IntranetCorporativa.Model.Noticias>
@using PagedList;
@using PagedList.Mvc;
@{
var format = "dddd, MMMM dd, yyyy";
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutPage.cshtml";
string principalTitulo = Model[0].Titulo;
string principalContenido = Model[0].Contenido;
DateTime principalFechaDesde = Convert.ToDateTime(Model[0].FeDesde);
DateTime principalFechaHasta = Convert.ToDateTime(Model[0].FeHasta);
}
<script type="text/javascript">
function changeDisplay(e) {
var principalTitulo = $(e).text();
var principalContenido = $(e).siblings(".vNoticiaContenido:first").html();
var principalFecha = $(e).siblings(".vNoticiaFecha:first").val();
$("#currentprincipalTitulo").html(principalTitulo);
$("#currentprincipalContenido").html(principalContenido);
$("#currentprincipalFecha").html(principalFecha);
}
</script>
<style>
.uppercase {
text-transform: uppercase;
}
.limit {
text-overflow: ellipsis;
word-wrap: break-word;
overflow: hidden;
max-height: 3em;
line-height: 1.7em;
}
</style>
<!-- CONTENIDO -->
<div class="col-md-12 main">
<div class="header sec-title-hd">
<div class="bg-calendar"></div>
<div class="col-md-7">
<h5 class="pull-left">NOTICIAS</h5>
<div>
<a href="dashboard.html" class="btn sky-blue n-radius-b"> <img src="slider/img/arrow-left.png"> VOLVER</a>
</div>
</div>
</div>
<div class="content-inter">
<div class="container-fluid sec-title-hd-sub">
<div class="row">
<div class="col-md-7">
<div>
<figure class="img_N">
<img id="currentprincipalImagen" src="#" class="img-responsive" alt="Here Principal IMG" />
<figcaption>
<p id="currentprincipalImagenTitulo">Here Img Description</p>
</figcaption>
</figure>
</div>
<div class="textnota">
<br>
<h5 id="currentprincipalTitulo" class="titulo_N uppercase">@principalTitulo</h5>
<p class="time">FeDesde: @principalFechaDesde.ToString(format)</p>
<p class="time">FeHasta: @principalFechaHasta.ToString(format)</p>
<p class="time">Hoy: @DateTime.Now.ToString(format)</p>
<div class="noti_P">
<p id="currentprincipalContenido">@principalContenido</p>
</div>
</div>
</div>
<div class="col-md-5">
<!-- Lado Derecho -->
@foreach (IntranetCorporativa.Model.Noticias n in Model)
{
<blockquote class="blockquote-nopadding bg-calendar-border-left">
<p class="time_f principalTitulo">@n.FeDesde.ToString(format)</p>
<a href="#" onclick="changeDisplay(this)" class="titulo_N">@n.Titulo</a>
<p class="text-justify limit vNoticiaContenido">@n.Contenido</p>
</blockquote>
}
Págnia @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
<div>
</div>
</div>
</div>
</div>
</div>
</div>
谢谢你的一切。
【问题讨论】:
标签: c# sql-server asp.net-mvc entity-framework linq