我是ASP.Net MVC的新手。我有三个模型Employee
,Address
,Store
。结构如下......
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
当我在视图中更新这些字段时,应更新所有模型。我知道如何使用多个模型而没有它们之间的关系。 感谢。
答案 0 :(得分: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);
}
}
然后,您的视图可以引用视图模型并像平常一样使用属性。
答案 1 :(得分: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>
请仔细研究这个&amp;让我知道您的反馈
答案 2 :(得分: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>
}
学生列表
ID 码 名称 报名号 @foreach(Model.Students的学生) { @ student.StudentId @ student.Code @学生姓名 @ student.EnrollmentNo }