c# - How to combine 2 tables using Entity Framework 6 and Linq in an MVC Project? -


i want know how data relationals tables.

i want images table named "noticias1" relationated "noticias" (noticias spanish word means news sorry university).

here diagram image

enter image description here

here "noticias1" table gets images contain news in table "noticias"

enter image description here

here "noticia" table contain 1 "noticia" means news in english enter image description here

here actual view img enter image description here

as can see shows "noticias" table have 1 news not problem.

now want images "noticias1" every news in table "noticias" show in view. (the named 1_0 featured img).

here controller

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 = n in entities.noticias                            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));         }     } 

here view

@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> 

thanks everyhing.

you need view model this:

internal class newsimagesviewmodel {     public string title{ get; set; }      public ienumerable<image> images { get; set; }      //... other properties } 

in controller:

ilist<newsimagesviewmodel> newsimageslist;  using (dbcontext dbcontext = new dbcontext()) {    newsimageslist = dbcontext.news        .select(n => new newsimagesviewmodel        {            title = n.title,            images = n.images,            // ... other properties may need        }        .tolist();                                          }  return view(newsimageslist); 

in view

@model ienumerable<your.namespace.newsimagesviewmodel> @foreach(var item in model) {  //.... } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -