2013-06-07 5 views
0

comment puis-je fusionner 2 table en 1 vue Ceci est mes modèles.comment joindre 2 table avec asp.net mvc4

namespace ProfileApplication.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class EMP 
    { 
     public string EMP_ID { get; set; } 
     public string EMP_NAME { get; set; } 
     public string EMP_LNAME { get; set; } 
    } 

    public partial class X_EMP_MAIL 
    { 
     public string EMP_ID { get; set; } 
     public string EMAIL { get; set; } 
    } 

ceci est mon Controtller.

public class profileController : Controller 
    { 
     // 
     // GET: /profile/ 
     ProfileEntities db = new ProfileEntities(); 

     public ActionResult Index() 
     { 
      return View(db.EMP.ToList()); 
     } 

     public ActionResult detail(string id = null) 
     { 

      var query = from EMP in db.EMP 

         join X_EMP_MAIL in db.X_EMP_MAIL on EMP.EMP_ID 
          equals X_EMP_MAIL.EMP_ID 

         where X_EMP_MAIL.EMP_ID == EMP.EMP_ID 
         select new joinproflie 
         { 
          EMP_ID = EMP.EMP_ID, 
          EMP_NAME = EMP.EMP_NAME, 
          EMP_LNAME = EMP.EMP_LNAME, 
          EMAIL  = X_EMP_MAIL.EMAIL 
         }; 

      if (query == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(query); 
     } 

ceci est mon avis

@model IEnumerable<ProfileApplication.Models.EMP> 
@foreach (var item in Model) 
    { 
     <tr> 
      <td>@item.EMP_ID </td> 
      <td>@item.EMP_NAME </td> 
      <td>@item.EMP_LNAME </td> 
      <td>@item.EMAIL </td> 
     <!-- --> 
     </tr> 
     <br/> 
    } 

comment dois-je faire?

Répondre

0

Vous pouvez créer une classe de modèle avec les propriétés EMP_ID, EMP_NAME, EMP_LNAME et EMAIL ou vous pouvez utiliser dynamic comme modèle.

1

Vous pouvez créer une classe plus viewmodel de combiner les deux viewmodels:

public partial class EMP_Details 
{ 
    public string EMP_ID { get; set; } 
    public string EMP_NAME { get; set; } 
    public string EMP_LNAME { get; set; } 
    public string EMAIL { get; set; } 
} 

Le changement du contrôleur pour revenir EMP_Details viewmodel. comme

public ActionResult detail(string id = null) 
    { 

     var query = from EMP in db.EMP 

        join X_EMP_MAIL in db.X_EMP_MAIL on EMP.EMP_ID 
         equals X_EMP_MAIL.EMP_ID 

        where X_EMP_MAIL.EMP_ID == EMP.EMP_ID 
        select new EMP_Details 
        { 
         EMP_ID = EMP.EMP_ID, 
         EMP_NAME = EMP.EMP_NAME, 
         EMP_LNAME = EMP.EMP_LNAME, 
         EMAIL  = X_EMP_MAIL.EMAIL 
        }; 

     if (query == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(query); 
    } 

sur la vue changement ViewModel Nom comme:

@model IEnumerable<ProfileApplication.Models.EMP_Details> 

et vous avez terminé!