2016-04-19 24 views
0

Ich bin neu in ASP.Net MVC. Ich habe drei Modelle Employee, Address, Store. Die Strukturen sind wie folgt ...Mehrere Modelle in einer Ansicht 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) 

Wie Sie alle drei Modelle in einem Controller verwenden und CRUD-Vorgänge im Controller durchführen. In der Ansicht von Employee möchte ich alle Felder aller drei Modelle zeigen, z.

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

Wenn ich diese Felder in der Ansicht aktualisiere, sollten alle Modelle aktualisiert werden. Ich weiß, wie man mehrere Modelle ohne Beziehung zwischen ihnen verwendet. Danke.

+2

eine Klasse schreiben mit 3 Eigenschaften und jede Eigenschaft ist vom Typ Ihr ​​jedes Modell. Diese Klasse umschließt also alle 3 Modelle, die diese Wrapper-Klasse verwenden, um damit zu spielen. Und diese Wrapperklasse heißt ViewModel in MVC ... Verwenden Sie dieses ViewModel, um Daten vom Controller an die View zu übergeben. –

Antwort

4

Hier kommt ein View-Modell zum Einsatz. Es ermöglicht Ihnen, Ihre Datenbank-Layer-Felder und -Logik von Ihren Präsentations-Layer-Feldern zu trennen.

Sie können mehrere Datenbankelemente kombinieren und nur die Elemente der einzelnen anzeigen, die Sie am Frontend anzeigen möchten.

Zum Beispiel könnten Sie die Ansicht Modell wie definieren:

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

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

Dann in Ihrem Controller, können Sie die Ansicht Modell erstellen und es in die Ansicht übergeben:

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

Dann Ihre Ansicht kann auf das Ansichtsmodell verweisen und die Eigenschaften wie gewohnt verwenden.

+0

'Das in das Dictionary übergebene Modellelement ist vom Typ 'Models.EmployeeDetails', aber für dieses Dictionary ist ein Modellelement erforderlich type 'System.Collections.Generic.IEnumerable''1 [Models.EmployeeDetails]'. 'Dieser Fehler tritt auf. Ich habe 'öffentliche Klasse EmployeeInfo: IEnumarable'. – DhavalR

+0

Sie werden wahrscheinlich die Typen im Konstruktor ändern müssen, um zu passen, ich habe nur das Konzept demonstriert. – Steve

+0

Können Sie mir bitte ein Beispiel zeigen? – DhavalR

0

Mitarbeiter haben Beziehung mit der Adresse & Store,

Modell

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

} 

-Controller

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

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

    } 
} 

Ansicht

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

Bitte schauen Sie in diesem & lassen Sie mich wissen Feedback

0

Dynamische Modell

Modelle

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

Wir können unser Modell als dynamisch (kein stark typisierte Modell) definieren mit das dynamische @ Model-Schlüsselwort.

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> 

} 

Schülerliste

Id -Code Namen Enrollment Kein @foreach (Studen t Student in Model.Students) { @ student.StudentId @ student.Code @ student.Name @ student.EnrollmentNo }
Verwandte Themen