0

我如何在 1 个视图中合并 2 个表这是我的模型。

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; }
    }

这是我的控制器。

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);
        }

这是我的看法

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

我应该怎么做?

4

2 回答 2

1

You can create one more viewmodel class to combine both 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; }
 }

On controller change it to return EMP_Details viewmodel. as

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);
    }

on view change ViewModel Name as:

@model IEnumerable<ProfileApplication.Models.EMP_Details>

and you are done!

于 2013-06-07T09:54:43.363 回答
0

您可以使用属性 EMP_ID、EMP_NAME、EMP_LNAME 和 EMAIL 创建模型类,也可以使用动态作为模型。

于 2013-06-07T09:32:03.520 回答