我为控制器创建了“部门和名称”支架。
我有以下项目结构:
具有以下数据库结构:
我想为这些情况创建一个视图页面。像这样:
插入表格名称(文本框)+部门名称(下拉列表框)+名称(下拉列表框)
部门
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
public class CompanyDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
}
}
名称.cs
namespace mvcAppraisalSystem.Models
{
public class Designation
{
[Key]
public int desgID { get; set; }
public string desgName { get; set; }
public string description { get; set; }
}
public class RoleDBContext : DbContext
{
public DbSet<Designation> Designations { get; set; }
}
}
有很多方法可以做到这一点。您可以创建一个视图模型,该模型将是该视图的特定模型。视图模型将包含部门和名称的列表,以及所选部门,名称和字段的ID。
然后,您可以对视图模型使用强类型视图。
可能的视图模型示例:
public class MyScenarioForm
{
[Key]
public string FormName { get; set; }
public int SelectedDesgId {get; set;}
public int SelectedDeptId { get; set; }
public IEnumerable<Designation> Designations { get; set; }
public IEnumerable<Department> Departments { get; set; }
// ... constructor or method that creates initial instance with Designations and Departments populated
}
处理下拉菜单的另一种方法是对这些下拉菜单使用部分视图。这样,此表单的视图模型就不需要具有名称和部门的列表。如果您要在其他地方重复使用这些下拉菜单,那么这也很好,那么您就可以重复使用这些局部视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句