c# - 实体的 MVC 模型设计

标签 c# asp.net asp.net-mvc

我试图在我的数据库中跟踪员工。

我有一个实体类:

[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 类?我不知道我在这里做错了什么。

最佳答案

你可以调用Html.EditorFor(m => m.Employee)

默认编辑器模板不考虑复杂类型属性。

关于c# - 实体的 MVC 模型设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727341/

相关文章:

c# - 如何在 WebApi 请求中获取 Controller 名称?

asp.net - 定义 ASP.NET SOAP Web 服务响应

c# - 使用 Html.RenderAction 将模型从 View 传递到 Controller 会导致模型为空

javascript - 使用隐藏输入字段替换数据库中的条目 - Entity Framework

c# - 没有你的车你就没有那么坚强。最快的查找列表

c# - null == x 和 x == null 之间的区别?

asp.net - 使用css将div移动到下一行

c# - 在回发时访问控制文本的首选方式

asp.net-mvc - MVC Razor @helper 函数无法正确递归?

c# - 如果代码中的任何地方使用了特定的外部函数,在编译时抛出错误?