2016-04-19 48 views
0

我是ASP.Net MVC的新手。我有三種型號Employee,Address,Store。該結構如下...一個視圖中的多個模型ASP.Net

EMPLOYEE:- 
    EmpID(PK), EmpName, Rank, StoreID, AddID 
STORE:- 
    StoreID(pk), BranchName, AddID 
ADDRESS:- 
    AddId(pk), Address, Phone, ID(fk EMPLOYEE.EmpID, fk STORE.StoreID) 

如何使用這三種車型在一個控制器以及如何在控制器執行CRUD操作。在Employee的視圖中,我想顯示所有三種模型的所有字段,例如

EmpID, EmpName, Rank, Store.BranchName, Address, Phone 

當我在視圖中更新這些字段時,應更新所有模型。我知道如何使用多個模型而沒有它們之間的關係。 謝謝。

+2

收件一類與3-屬性和每個屬性是你的每個模型的類型。所以這個類包裝了所有3個模型使用這個包裝類來玩。此包裝類在MVC中被稱爲ViewModel ...使用此視圖模型將數據從控制器傳遞到視圖.. –

回答

4

這是視圖模型派上用場的地方。它允許您將數據庫層字段和邏輯與表示層字段分開。

您可以組合多個數據庫實體,只顯示要在前端顯示的每個實體的元素。

例如,你可以定義你的瀏覽模式,如:

public class EmployeeInfo 
{ 
    public string EmployeeName {get;set;} 
    // other properties 

    public EmployeeInfo(Employee emp, Store store, Address address) 
    { 
     EmployeeName = emp.EmpName; 
     // assign other properties 
    } 
} 

然後,在你的控制器,你可以創建視圖模型,並將其傳遞給視圖:

public class EmployeeController 
{ 
    public IActionResult Index() 
    { 
     var empInfo = new EmployeeInfo(employee, store, address); // retrieved from database somehow 
     return View(empInfo); 
    } 
} 

然後,您的視圖可以引用視圖模型並像通常那樣使用屬性。

+0

'傳入字典的模型項類型爲'Models.EmployeeDetails',但此字典需要模型項鍵入'System.Collections.Generic.IEnumerable''1 [Models.EmployeeDetails]'。'出現此錯誤。我做了'公共類EmployeeInfo:IEnumarable'。 – DhavalR

+0

您可能需要更改構造函數中的類型才能匹配,我只是在展示這個概念。 – Steve

+0

你能告訴我一個例子嗎? – DhavalR

0

員工有地址&商店,

型號

namespace Tester.Models 
{ 
    public class EMPLOYEEVIEWMODEL 
    { 
     public int EmpID { get; set; } 
     public string EmpName { get; set; } 
     public int Rank { get; set; } 
     public ADDRESSVIEWMODEL Address { get; set; } 
     public STOREVIEWMODEL Store { get; set; } 

    } 

    public class STOREVIEWMODEL 
    { 
     public int StoreID { get; set; } 
     public string BranchName { get; set; }  
    } 
    public class ADDRESSVIEWMODEL 
    { 
     public int AddId { get; set; } 
     public string Address { get; set; } 
     public string Phone { get; set; }   
    } 

} 

控制器

namespace Tester.Controllers 
{ 
    public class EmployeeController : Controller 
    { 
     // 
     // GET: /Employee/ 

     public ActionResult Add() 
     { 
      return View(); 
     } 

    } 
} 

查看

關係
@using Tester.Models; 
@model EMPLOYEEVIEWMODEL 
@{ 
    Layout = null; 
} 
<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Add</title> 
</head> 
<body> 
    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 
     <div class="form-group"> 
      @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-4" }) 
      <div class="col-md-4"> 
       @Html.TextAreaFor(model => model.EmpName, new { @class = "form-control input-sm" }) 
       @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "help-block" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Rank, htmlAttributes: new { @class = "control-label col-md-4" }) 
      <div class="col-md-4"> 
       @Html.TextAreaFor(model => model.Rank, new { @class = "form-control input-sm" }) 
       @Html.ValidationMessageFor(model => model.Rank, "", new { @class = "help-block" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Store.BranchName, htmlAttributes: new { @class = "control-label col-md-4" }) 
      <div class="col-md-4"> 
       @Html.TextAreaFor(model => model.Store.BranchName, new { @class = "form-control input-sm" }) 
       @Html.ValidationMessageFor(model => model.Store.BranchName, "", new { @class = "help-block" }) 
      </div> 
     </div>} 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Address.Address, htmlAttributes: new { @class = "control-label col-md-4" }) 
     <div class="col-md-4"> 
      @Html.TextAreaFor(model => model.Address.Address, new { @class = "form-control input-sm" }) 
      @Html.ValidationMessageFor(model => model.Address.Address, "", new { @class = "help-block" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Address.Phone, htmlAttributes: new { @class = "control-label col-md-4" }) 
     <div class="col-md-4"> 
      @Html.TextAreaFor(model => model.Address.Phone, new { @class = "form-control input-sm" }) 
      @Html.ValidationMessageFor(model => model.Address.Phone, "", new { @class = "help-block" }) 
     </div> 
    </div> 
    <input id="Submit" type="submit" value="submit" /> 
</body> 
</html> 

請看看這個&讓我知道您的反饋

0

使用動態模型

模型

public class Teacher 
{ 
    public int TeacherId { get; set; } 
    public string Code { get; set; } 
    public string Name { get; set; } 
} 

public class Student 
{ 
    public int StudentId { get; set; } 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public string EnrollmentNo { get; set; } 
} 


Controller Code 

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     ViewBag.Message = "Welcome to my demo!"; 
     dynamic mymodel = new ExpandoObject(); 
     mymodel.Teachers = GetTeachers(); 
     mymodel.Students = GetStudents(); 
     return View(mymodel); 
    } 
} 

我們可以定義我們的動態模型(不是一個強類型的模型)使用@model動態關鍵字。

View Code 

    @using MultipleModelInOneView; 

    @model dynamic 

    @{ 

    ViewBag.Title = "Home Page"; 

    } 

    <h2>@ViewBag.Message</h2> 



    <p><b>Teacher List</b></p> 



    <table> 

    <tr> 

    <th>Id</th> 

    <th>Code</th> 

    <th>Name</th> 

</tr> 

@foreach (Teacher teacher in Model.Teachers) 

{ 

    <tr> 

     <td>@teacher.TeacherId</td> 

     <td>@teacher.Code</td> 

     <td>@teacher.Name</td> 

    </tr> 

} 

學生名單

標識 代碼 名稱 招生沒有 @foreach(斯圖登在Model.Students噸學生) { @ student.StudentId @ student.Code @ student.Name @ student.EnrollmentNo }
相關問題