我正在尝试跟踪我的数据库中的员工。
我有一个实体类:
[Table(Name = "Employees")]
public class Employee
{
[HiddenInput(DisplayValue = false)]
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int EmployeeID { get; set; }
[Column]
[Required]
public string FirstName { get; set; }
[Column]
[Required]
public string LastName { get; set; }
[Column]
[Required]
public string Email { get; set; }
}
我正在尝试创建一个将员工添加到数据库的页面。所以我创建了一个ViewModel:
public class EmployeesAddViewModel
{
public Employee employee { get; set; }
}
我创建了这样的ViewModel,因为我计划稍后在模型中添加其他东西,而不是在Employees表中。我只是想从这开始看看是否一切正常。
我有我的页面:
<%@ Page Inherits="System.Web.Mvc.ViewPage<EmployeesAddViewModel>" ... %>
<% using (Html.BeginForm("Add", "Employees", FormMethod.Post)) { %>
<%: Html.EditorForModel() %>
<input type="submit" value="Save" />
<% } %>
但是当我查看我的页面时,它没有显示我的模型的任何表单字段,它没有显示任何内容。
我是否需要在模型中为每个实体字段添加字段,而不是仅在模型中包含整个Employee类?我不知道我在这里做错了什么。
答案 0 :(得分:3)
您可以致电Html.EditorFor(m => m.Employee)
默认编辑器模板不考虑复杂类型属性。