【问题标题】:How to combine 2 tables using Entity Framework 6 and Linq in an MVC Project?如何在 MVC 项目中使用 Entity Framework 6 和 Linq 组合 2 个表?
【发布时间】:2016-05-15 23:16:01
【问题描述】:

我想知道如何从关系表中获取数据。

我想从名为“Noticias1”的表中获取与“Noticias”相关的图像(Noticias 是一个西班牙语单词,意思是新闻对不起,但它适用于我的大学)。

这里是图表图像

这是我的“Noticias1”表,它获取将包含表“Noticias”中新闻的图像

这是我的“Noticia”表,其中仅包含 1 个“Noticia”,表示英文新闻

这里是实际视图 IMG

如您所见,它仅显示“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


    【解决方案1】:

    您将需要这样的视图模型:

    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)
    {
     //....
    }
    

    【讨论】:

    • 我也建议将 ViewModel 和 BusinessModel 类分开
    【解决方案2】:

    首先,帮自己一个忙,为您的类和属性使用更好的名称。您可以根据需要在 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 (var image in n.Noticias2) 来显示图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-19
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多