![](/img/trans.png)
[英]How to Update multiple tables with foreign keys using LINQ expression ASP.NET MVC Entity Framework
[英]How to combine 2 tables using Entity Framework 6 and Linq in an MVC Project?
我想知道如何从关系表中获取数据。
我想从与“ Noticias”相关的名为“ Noticias1”的表中获取图像(Noticias是西班牙语,表示对不起新闻,但这对我的大学而言)。
这里的图图像
在这里,我的“ Noticias1 ”表获取的图像将包含表“ Noticias”中的新闻
在这里,我的“ Noticia ”表仅包含1个“ Noticia”,这意味着英语新闻
如您所见,它仅显示“ Noticias”表,该表只有1条新闻,这不是问题。
现在,我想将所有从“ Noticias1”到“新闻”表中每个新闻的图像显示在我的视图中。 (名称为1_0的将是精选img)。
这是我的控制器
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>
谢谢你的一切。
您将需要一个这样的视图模型:
internal class NewsImagesViewModel
{
public string Title{ get; set; }
public IEnumerable<Image> Images { get; set; }
//... some other properties
}
在控制器中:
IList<NewsImagesViewModel> newsImagesList;
using (DbContext dbContext = new DbContext())
{
newsImagesList = dbContext.News
.Select(n => new NewsImagesViewModel
{
Title = n.Title,
Images = n.Images,
// ... some other properties you may need
}
.ToList();
}
return View(newsImagesList);
在视图中
@model IEnumerable<Your.Namespace.NewsImagesViewModel>
@foreach(var item in Model)
{
//....
}
首先,请帮自己一个忙,并为类和属性使用更好的名称。 您可以根据需要在edmx设计器中对其进行修改,更新不会破坏所做的更改。 将Noticias1
更改为相当于NewsImage的西班牙语,然后重命名导航属性。
其次,使用Include
来获取新闻图像:
var noticias = from n in entities.Noticias.Include(n => n.Noticias2) // TODO: rename!!!
where n.Activo && n.FeDesde <= DateTime.Now && DateTime.Now <= n.FeHasta
select n;
然后在@foreach (IntranetCorporativa.Model.Noticias n in Model)
某个位置@foreach (IntranetCorporativa.Model.Noticias n in Model)
您将需要一个@foreach (var image in n.Noticias2)
来显示图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.