Friday 21 June 2019

Pass (Send) Multiple Models to one View in ASP.Net MVC



Model
Following are the two Model classes.
Customer
The following Model class will be used to populate data from Customers Table.
public class Customer
{
    public string CustomerId { getset; }
    public string CustomerName { getset; }
    public string City { getset; }
}

Employee
The following Model class will be used to populate data from Employees Table.
public class Employee
{
    public string EmployeeId { getset; }
    public string EmployeeName { getset; }
    public string City { getset; }
}



Controller
The Controller consists of an Index Action method. Inside this Action method, first an object of ExpandoObject class is created and it's instance is assigned to a variable of type Dynamically

NoteExpandoObject  allows to add and remove objects dynamically at runtime. It is member of System.Dynamic namespace.

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        dynamic model = new System.Dynamic.ExpandoObject();
        model.Customers = GetCustomers();
        model.Employees = GetEmployees();
        return View(model);
    }

    private static List<Customer> GetCustomers()
    {
        List<Customer> customers = new List<Customer>();
        string query = "SELECT CustomerID, ContactName, City FROM Customers";
        string constr = ConfigurationManager.ConnectionStrings["Con1"].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 Customer
                        {
                            CustomerId = sdr["CustomerID"].ToString(),
                            CustomerName = sdr["ContactName"].ToString(),
                            City = sdr["City"].ToString()
                        });
                    }
                }
                con.Close();
                return customers;
            }
        }
    }

    private static List<Employee> GetEmployees()
    {
        List<Employee> employees = new List<Employee>();
        string query = "SELECT EmployeeID, EmployeeName [Name], City FROM Employees";
        string constr = ConfigurationManager.ConnectionStrings["Con1"].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 Employee
                        {
                            EmployeeId = sdr["EmployeeID"].ToString(),
                            EmployeeName = sdr["Name"].ToString(),
                            City = sdr["City"].ToString()
                        });
                    }
                    con.Close();
                    return employees;
                }
            }
        }
    }
}


View
In View, I' ll need to import the namespace for accessing the Model classes.
Then we need to declare the Model for the View as dynamically.
For displaying the records, two HTML Tables are used and by iterating over the Generic List Collection of Model objects, rows are added to the HTML Tables.

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

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerID</th>
            <th>Contact Name</th>
            <th>City</th>
        </tr>
        @foreach (Customer customer in Model.Customers)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.CustomerName</td>
                <td>@customer.City</td>
            </tr>
        }
    </table>
    <hr/>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>EmployeeID</th>
            <th>Employee Name</th>
            <th>City</th>
        </tr>
        @foreach (Employee employee in Model.Employees)
        {
            <tr>
                <td>@employee.EmployeeId</td>
                <td>@employee.EmployeeName</td>
                <td>@employee.City</td>
            </tr>
        }
    </table>
</body>
</html>


Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...