Friday, 17 November 2017

How to Display Data in a single view from Multiple Tables in ASP.NET MVC .

How to Display Data in a single view from Multiple Tables in ASP.NET MVC .

Description:

In this example we explain that how to display data in single Razor view from multiple Tables in Asp.Net MVC.or how to display multiple Model Data in Single Razor View in MVC.or how to display multiple Tables Data in Single Razor View in MVC.or how to use multiple Models in single Razor View in MVC.

Models:

  public class CustomerModel
    {
        public string CustomerId { get; set; }
        public string CustomerName { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
    public class EmployeeModel
    {
        public string EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }

Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            dynamic model = new ExpandoObject();
            model.Customers = GetCustomers();
            model.Employees = GetEmployees();
            return View(model);
        }

        private static List<CustomerModel> GetCustomers()
        {
            List<CustomerModel> customers = new List<CustomerModel>();
            string query = "SELECT TOP 10 CustomerID, ContactName, City, Country FROM Customers";
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            customers.Add(new CustomerModel
                            {
                                CustomerId = sdr["CustomerID"].ToString(),
                                CustomerName = sdr["ContactName"].ToString(),
                                City = sdr["City"].ToString(),
                                Country = sdr["Country"].ToString()
                            });
                        }
                    }
                    con.Close();
                    return customers;
                }
            }
        }

        private static List<EmployeeModel> GetEmployees()
        {
            List<EmployeeModel> employees = new List<EmployeeModel>();
            string query = "SELECT EmployeeID, (FirstName + ' ' + LastName) [Name], City, Country FROM Employees";
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            employees.Add(new EmployeeModel
                            {
                                EmployeeId = sdr["EmployeeID"].ToString(),
                                EmployeeName = sdr["Name"].ToString(),
                                City = sdr["City"].ToString(),
                                Country = sdr["Country"].ToString()
                            });
                        }
                        con.Close();
                        return employees;
                    }
                }
            }
        }
    }

View: 

@using Multiple_Model_MVC.Models
@model dynamic
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Display data in Single View from Multiple Tables in ASP.Net MVC</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerID</th>
            <th>Contact Name</th>
            <th>City</th>
            <th>Country</th>
        </tr>
        @foreach (CustomerModel customer in Model.Customers)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.CustomerName</td>
                <td>@customer.City</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
    <hr/>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>EmployeeID</th>
            <th>Employee Name</th>
            <th>City</th>
            <th>Country</th>
        </tr>
        @foreach (EmployeeModel employee in Model.Employees)
        {
            <tr>
                <td>@employee.EmployeeId</td>
                <td>@employee.EmployeeName</td>
                <td>@employee.City</td>
                <td>@employee.Country</td>
            </tr>
        }
    </table>
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment