0
我一直在使用viewbags來使用剃刀填充我的視圖上的下拉列表,我想知道是否有反正清理這段代碼。我一直這樣做了一段時間,我討厭它的外觀,它使得所有的代碼看起來非常混亂,並且必須在Get和Post操作中複製它。有沒有其他方式可以做到這一點?清理GET和POST中的ViewBag
下面是代碼的樣子:
[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");
}
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2014-09-05 22:37:05
在POST方法中,如果您要返回視圖(即ModelState無效),則只需分配ViewBag屬性。你總是可以把這個代碼放在一個單獨的方法中(比如'ConfigureViewModel()')來避免重複代碼並根據需要調用它,但是你應該考慮在視圖模型中包含這些屬性,而不是使用ViewBag。 – 2014-09-06 00:10:59