简体   繁体   English

一个视图中的多个模型 ASP.Net

[英]Multiple models in one view ASP.Net

I am new to ASP.Net MVC.我是 ASP.Net MVC 的新手。 I have three models Employee , Address , Store .我有三个模型EmployeeAddressStore The structures are as follows...结构如下...

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

How to use all three models in one controller and how to perform CRUD operations in controller.如何在一个控制器中使用所有三种模型以及如何在控制器中执行 CRUD 操作。 In the view of Employee , I want to show all the fields of all three of models, egEmployee的视图中,我想显示所有三个模型的所有字段,例如

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

When I update these fields in view, all models should be updated.当我在视图中更新这些字段时,应该更新所有模型。 I know if how to use multiple models without relationship between them.我知道如何在没有关系的情况下使用多个模型。 Thanks.谢谢。

This is where a view model comes in handy.这就是视图模型派上用场的地方。 It allows you to separate your database layer fields and logic from your presentation layer fields.它允许您将数据库层字段和逻辑与表示层字段分开。

You can combine several database entities and only expose the elements of each that you want to display on the front end.您可以组合多个数据库实体,并仅公开您希望在前端显示的每个实体的元素。

For example, you might define your view model like:例如,您可以像这样定义视图模型:

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

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

Then, in your controller, you can create the view model and pass it to the view:然后,在您的控制器中,您可以创建视图模型并将其传递给视图:

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

Then, your view can reference the view model and use the properties like you would normally.然后,您的视图可以像往常一样引用视图模型并使用属性。

Employee have relationship with the address & Store,员工与地址和商店有关系,

Model型号

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

    }
}

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>

Kindly look into this & let me know your feedback请仔细研究并让我知道您的反馈

Using Dynamic Model使用动态模型

Models型号

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

We can define our model as dynamic (not a strongly typed model) using the @model dynamic keyword.我们可以使用@model dynamic 关键字将我们的模型定义为动态的(不是强类型模型)。

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>

}

Student List学生名单

Id Code Name Enrollment No @foreach (Student student in Model.Students) { @student.StudentId @student.Code @student.Name @student.EnrollmentNo } Id Code Name Enrollment No @foreach (Student student in Model.Students) { @student.StudentId @student.Code @student.Name @student.EnrollmentNo }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM