ASP .Net MVC 模型 - ViewModel - 视图
Posted
技术标签:
【中文标题】ASP .Net MVC 模型 - ViewModel - 视图【英文标题】:ASP .Net MVC Model - ViewModel - View 【发布时间】:2021-04-23 20:21:29 【问题描述】:我在视图中使用模型属性时遇到问题。 我的模型:
-
Employee.cs
namespace App.Data.Models
public class Employee
public int Id get; set;
public string FirstName get; set;
public string LastName get; set;
public class Project
public ProjectType ProjectType get; set;
public bool isSelected get; set;
-
ProjectType.cs
namespace App.Data.Models
public enum ProjectType
Project1,
Project2,
Project3,
Project4,
-
EmployeeViewModel.cs
namespace App.Data.Models
public class EmployeeViewModel
public Employee Employee get; set;
public List<Project> Project get; set;
我的看法: 4. Create.cshtml - 在创建视图中我无法验证@Html.ValidationMessageFor - 当我注释掉用于项目的第三组时 - 消息显示。但是,如果该组留下来,我就会出错。
@model App.Data.Models.EmployeeViewModel
@using App.Data.Models;
@
ViewBag.Title = "Create";
@using (Html.BeginForm())
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new @class = "text-danger" )
<div class="form-group">
@Html.LabelFor(model => model.Employee.FirstName, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Employee.FirstName, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Employee.FirstName, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Employee.LastName, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Employee.LastName, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Employee.LastName, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Project, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@for (int i = 0; i < Model.Project.Count; i++)
<div class="col-md-10">
@Html.CheckBoxFor(m => m.Project[i].isSelected)
@Html.DisplayFor(m => m.Project[i].ProjectType)
</div>
</div>
@Html.ValidationMessageFor(model => model.Project, "", new @class = "text-danger" )
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
-
Details.cshtml - 在这个视图中我不知道如何访问属性 - @Model.Employee.LastName - 不起作用
@model App.Data.Models.EmployeeViewModel
@
ViewBag.Title = "Details";
<h2>@Model.Employee.FirstName @Model.Employee.LastName</h2>
<div>
<h4>Employee</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Employee.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.Employee.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Employee.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.Employee.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Project)// Here I don't know how to get to the projects which were checked in create view
</dt>
<dd>
@Html.DisplayFor(model => model.Project)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new id = Model.Employee.Id ) |//This line also doesn't work
@Html.ActionLink("Back to List", "Index")
</p>
【问题讨论】:
你使用的是什么版本的 MVC? 我正在使用 MVC 5 这是你的学生项目还是你想赚钱? 学生项目 我为什么要问,因为我不喜欢使用枚举 ProjectType。最好创建一个特殊的表。但是如果你不能改变我们可以继续枚举。 【参考方案1】:我认为我们可以从用另一个 Project db 表替换您的枚举和您的项目开始。创建一个新类:
public class Project
[Key]
public int Id get; set;
public string Name get; set;
public string Descriptionget; set;
您还需要 ProjectViewModel 类。它将用于创建您的复选框列表。
public class ProjectViewModel
public int ProjectId get; set;
public string ProjectName get; set;
public bool IsSelected get; set;
您的 EmployeeViewModel 类将是这样的:
public class EmployeeViewModel
public Employee Employee get; set;
public ProjectViewModel[] ProjectViewModels get; set;
var employeeViewModel= new EmployeeViewModel
Employee=new Employee(),
ProjectViewModels=dbContext.Projects.Select (p=>
new ProjectViewModelId=p.Id, Name=p.Name ).ToArray();
你的 Employee 类是这样的:
public class Employee
[Key]
public int Id get; set;
public string FirstName get; set;
public string LastName get; set;
public virtual ICollection<Project> Projects get; set;
由于员工有多个项目,您需要第三张表:
public class EmployeeProject
[Key]
public int Id get; set;
[ForeignKey]
public int EmployeeId get; set;
[ForeignKey]
public int ProjectId get; set;
【讨论】:
好吧,所以当我为员工创建例如 Deatails 视图时,我将使用两个模型 - 员工和项目。那么我应该使用 EmployeeProject 吗?如果是这样,如何在此视图中获取 FirstName 或 LastName? 您不需要将 EmployeeProject 添加到您的 ViewModel。使用已经存在的视图模型。在您的操作类中,您必须从 Projects 表中选择所有项目并将其分配给 EmployeeViewModel 的 Projects 列表。在您的详细信息视图中,您用这个枚举列表替换。 您的 EmploeeViewModel 保留所有项目的列表,但您的 Employee 类仅在为该员工项目选择的列表中。立即尝试修复您的详细信息。 那么我怎样才能为创建视图的复选框创建循环呢?我不需要 isSelected 属性吗? 是的,您必须将 Project 转换为 ProjectViewModel。看我的回答。以上是关于ASP .Net MVC 模型 - ViewModel - 视图的主要内容,如果未能解决你的问题,请参考以下文章