在GET和POST中清理ViewBag

时间:2014-09-05 22:07:09

标签: c# asp.net asp.net-mvc razor viewbag

我一直使用viewbags来使用razor填充我的视图中的下拉列表,我想知道是否有任何方法来清理此代码。我已经这样做了一段时间,我讨厌它看起来的方式,它使所有代码看起来非常混乱,并且必须在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");
}

1 个答案:

答案 0 :(得分:1)

实施视图模型模式。

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

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