0

how can i merge 2 table in 1 view This is my Models.

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

this is my 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);
        }

this is my view

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

how should i do?

2 Answers 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!

Sign up to request clarification or add additional context in comments.

Comments

0

You can either create a model class with the properties EMP_ID, EMP_NAME, EMP_LNAME and EMAIL, or you can use dynamic as your model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.