ASP .Net MVC 模型 - 视图模型 - 视图

ASP .Net MVC Model - ViewModel - View

我在使用我的视图中的模型属性时遇到问题。 我的模特:

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

  1. ProjectType.cs
namespace App.Data.Models
{ 
    public enum ProjectType
    {
       Project1,
       Project2,
       Project3,
       Project4,
    }
}

  1. 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>
  1. 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>

我认为我们可以从用另一个项目数据库替换您的枚举和项目开始 table。创建一个新的 class:


public class Project
{ 
[Key]
public int Id {get; set;}
public string Name {get; set;}
public string Description{get; set;}
}

您还需要 ProjectViewModel class。它将用于创建您的复选框列表。

public class ProjectViewModel
{ 
public int ProjectId {get; set;}
public string ProjectName {get; set;}
public bool IsSelected {get; set;}
}

您的 EmployeeViewModel class 将是这样的:

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

而您的员工 class 是这样的:

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

并且由于员工有多个项目,您需要第三个 table:


 public class EmployeeProject
    {
    [Key]
   public int Id { get; set; }
   [ForeignKey]
   public int EmployeeId { get; set; }
 [ForeignKey]
   public int ProjectId { get; set; }
     }