清理ViewBag';s在GET和POST中

本文关键字:GET POST ViewBag 清理 | 更新日期: 2023-09-27 18:20:46

我一直在使用viewbags来填充我的视图上的下拉列表,我想知道是否有办法清理这些代码。我已经做了一段时间了,我讨厌它的外观,它让所有的代码看起来非常混乱,并且不得不在Get和Post操作中重复它。还有别的办法吗?

以下是代码的样子:

[HttpGet]
public ActionResult EmployeeEdit(int id)
{
    var employee = _employeeDb.EmployeeMasters.FirstOrDefault(e => e.ID == id);
    var nextReview = _employeeDb.EmployeeReviews.OrderByDescending(e => e.ReviewDate).FirstOrDefault(e => e.EmployeeNo == employee.EmployeeNumber);
    ViewBag.NextReviewDate = nextReview != null ? nextReview.ReviewDate.Value.ToShortDateString() : "Add Review Date";
    ViewBag.CompanyList = _selectListLib.GetCompanies((int)employee.CompanyNumber);
    ViewBag.BranchList = _selectListLib.GetBranches((int)employee.CompanyNumber, employee.Branch);
    ViewBag.departmentList = employee.DepartmentNumber != null ? _selectListLib.GetDepartments((int)employee.CompanyNumber, (int)employee.DepartmentNumber) : _selectListLib.GetDepartments((int)employee.CompanyNumber);
    ViewBag.EmployeeTypesList = _selectListLib.GetEmployeeTypes(employee.EmployeeType);
    ViewBag.PayTypesList = _selectListLib.GetPayTypes(employee.PayType);
    ViewBag.StatusList = employee.StatusFlag != null ? _selectListLib.GetStatusTypes((bool)employee.StatusFlag) : _selectListLib.GetStatusTypes();
    ViewBag.LastEditLabel = MakeLastEditString((int)employee.EmployeeNumber);
    ViewBag.Managers = employee.ManagerEmpNo != null ? _selectListLib.GetManagers((int) employee.CompanyNumber, (int) employee.ManagerEmpNo) : _selectListLib.GetManagers((int)employee.CompanyNumber);
    return View(employee);
}
[HttpPost]
public ActionResult EmployeeEdit(Models.EmployeeMaster employee)
{
    var nextReview = _employeeDb.EmployeeReviews.OrderByDescending(e => e.ReviewDate).FirstOrDefault(e => e.EmployeeNo == employee.EmployeeNumber);
    ViewBag.NextReviewDate = nextReview != null ? nextReview.ReviewDate.Value.ToShortDateString() : "Add Review Date";
    ViewBag.CompanyList = _selectListLib.GetCompanies((int)employee.CompanyNumber);
    ViewBag.BranchList = _selectListLib.GetBranches((int)employee.CompanyNumber, employee.Branch);
    ViewBag.departmentList = employee.DepartmentNumber != null ? _selectListLib.GetDepartments((int)employee.CompanyNumber, (int)employee.DepartmentNumber) : _selectListLib.GetDepartments((int)employee.CompanyNumber);
    ViewBag.EmployeeTypesList = _selectListLib.GetEmployeeTypes(employee.EmployeeType);
    ViewBag.PayTypesList = _selectListLib.GetPayTypes(employee.PayType);
    ViewBag.StatusList = employee.StatusFlag != null ? _selectListLib.GetStatusTypes((bool)employee.StatusFlag) : _selectListLib.GetStatusTypes();
    ViewBag.Managers = employee.ManagerEmpNo != null ? _selectListLib.GetManagers((int)employee.CompanyNumber, (int)employee.ManagerEmpNo) : _selectListLib.GetManagers((int)employee.CompanyNumber);
    if (!ModelState.IsValid) return View(employee);
    var result = _dbDataManipulation.UpdateEmployee((int)employee.CompanyNumber, employee.Initials, employee.FirstName, employee.LastName, (int)employee.DepartmentNumber, employee.EmployeeNumber.ToString(),
        employee.EmployeeType, (bool)employee.EnterTimeFl, Convert.ToDateTime(employee.StartDate), employee.PayType, (decimal)employee.Rate, (int)employee.UnionNo, employee.Branch, employee.G2ID, employee.JobTitle, CurrentUserName(),(bool)employee.StatusFlag, employee.MiddleName,(int)employee.ManagerEmpNo)
        ;
    ViewBag.Message = result;
    if (result != null)
        return View(employee);
    return RedirectToAction("Default");
}

清理ViewBag';s在GET和POST中

实现视图模型模式。

您可以填充视图模型对象,并将其发送到将绑定到该对象的视图。您可以在这个视图模型的构造函数或您可能创建的转换器类中执行许多丑陋的视图特定逻辑。

http://nerddinnerbook.s3.amazonaws.com/Part6.htm