ASP.net MVC - 在模型

时间:2017-03-28 10:31:30

标签: asp.net-mvc

我是MVC的新手。我有3个模型:部门,单位,员工。

public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public int ManagerId { get; set; }
}

public class Unit
{
public int UnitID { get; set; }
public string UnitName { get; set; }
public int ManagerId
}

public class Employees
{
public int EmployeeId  { get; set; }
public string EmployeeName  { get; set; }
}

我想在单个视图中编辑对象Unit和Department。我使用Structure视图模型:

public class StructureModel
{   
    public IEnumerable<Departments> Departments { get; set; }
    public IEnumerable<Units> Units { get; set; }
    public IEnumerable<Employee> Employees { get; set; }
}

我希望在一个视图中使用一列ManagerName而不是ManagerId(ManagerName == EmployeeName,其中Unit.ManagerId == Employee.EmployeeId)显示Departments和Units。我的控制器:

private RegistraiContext db = new RegistraiContext();
    public ActionResult Index()        {
        var model = new StructureModel();
        model.Departments = db.Departmentss.ToList().OrderBy(r => r.DepartmId);
        model.Units = db.Unitss.ToList().OrderBy(r => r.UnitId);
        return View(model);
    }

最后我的观点(我使用Grid.Mvc)

@model Registrai.Models.StructureModel
@using GridMvc.Html
@Html.Grid(Model.Unit).Columns(columns =>
{
columns.Add(model => model.UnitId).Titled("UniId")
columns.Add(model => model.UnitName).Titled("UnitName");
columns.Add(model => model.ManagerName).Titled("Manager");
}
.........................

其中(在模型Unit中,在StructureModel中或在控制器中)以及如何创建查询以从DB检索ManagerName以及如何修改模型Unit和Department以添加属性ManagerName? 谢谢

0 个答案:

没有答案