Saturday 19 July 2014

How to use Multiple Models in Single View in MVC

multiple model in a single razor view in mvc



Description:-

In this example we explain that how to define two Model in a single razor view or how to handle two model for single razor view in mvc.

We all know that as a developer sometime we have required two models in single view in many situations. Suppose in a razor view we have define register form with many field like first name, last name etc... and also define model to validate all field like first name. last name and when user fill data and click on submit button then this record will be bind to web grid below the same razor view so here we required two model one model for validation and second model for web grid binding,editing,deleting and define property related used in web grid.

Sorting Row data in gridview Gridview Sorting 

How to handle Concurrency in Linq to Sql Concurrency Example

So to handle this type of situation we have to define our code same like below

create two class in a model folder like belows

public class Company
{
    public int CompanyId { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

public class Employee
{
    public int EmployeeId { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public string EmployeeNo { get; set; }

}
private List<Company> GetCompanys()
{
    List<Company> Companys = new List<Company>();
    Companys.Add(new Company { CompanyId = 1, Code = "c001", Name = "Reliance" });
    Companys.Add(new Company { CompanyId = 2, Code = "c002", Name = "Hyundai" });
    Companys.Add(new Company { CompanyId = 3, Code = "c003", Name = "TATA" });
    return Companys;
}

public List<Employee> GetEmployees()
{
    List<Employee> Employees = new List<Employee>();
    Employees.Add(new Employee { EmployeeId = 1, Code = "e001", Name = "kirit kapupara", EmployeeNo = "420" });
    Employees.Add(new Employee { EmployeeId = 2, Code = "e002", Name = "pintu paghadar", EmployeeNo = "305" });
    Employees.Add(new Employee { EmployeeId = 3, Code = "e003", Name = "hiren patel", EmployeeNo = "101" });
    return Employees;
}

 Homecontroller.cs:-

public class HomeController : Controller
{
    public
 ActionResult Index()
    {
        ViewBag.Message = "Demostrate two Model in a single View"
;
        dynamic
 mymodel = new ExpandoObject();
        mymodel.Company = GetCompany();
        mymodel.Employee = GetEmployees();
        return
 View(mymodel);
    }
}

Index.cshtml:-

@using MultipleModelInOneView;
@model dynamic
@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>

<p><b>Company List</b></p>

<table>
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
    </tr>
    @foreach (Company com in Model.Company)
    {
        <tr>
            <td>@ com .EmployeeId</td>
            <td>@ com .Code</td>
            <td>@ com .Name</td>
        </tr>
    }
</table>

<p><b>Employee List</b></p>

<table>
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
        <th>Employee No</th>
    </tr>
    @foreach (Employee emp in Model.Employee)
    {
        <tr>
            <td>@ emp .EmployeeId</td>
            <td>@ emp .Code</td>
            <td>@ emp .Name</td>
            <td>@ emp .EmployeeNo</td>
        </tr>
    }
</table>


6 comments:

  1. what is dynamic mymodel = new ExpandoObject(); plz help me.......

    ReplyDelete
  2. It is a class and u have to include namespace using system.dynamic in your page thank you. .

    ReplyDelete
  3. Nice Post Really very usefull......

    ReplyDelete
  4. Compiler Error Message: CS0234: The type or namespace name 'MultipleModelInOneView' does not exist in the namespace 'System.Dynamic' (are you missing an assembly reference?)

    ReplyDelete
  5. Bad Solution...copied with errors

    ReplyDelete
  6. I cant trace the reference @using MultipleModelInOneView;

    ReplyDelete