如何解决ASP .Net MVC 模型 - ViewModel - 视图
我在视图中使用模型的属性时遇到问题。 我的模型:
- 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>
@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>
解决方法
我认为我们可以从用另一个 Project db 表替换您的枚举和您的项目开始。创建一个新类:
public class Project
{
[Key]
public int Id {get; set;}
public string Name {get; set;}
public string Description{get; 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 ProjectViewModel{Id=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; }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。