此操作是否過於冗餘 - 是否有更好的方法來簡化它?ASP.NET MVC操作中的冗餘消除操作
[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChangePassword(string oldPassword, string newPassword, string confirmPassword)
{
var oldPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(oldPassword);
oldPasswordValidationResults.Where(r => !r.Passed)
.Each(r => ModelState.AddModelError("OldPassword", "Please enter your old password."));
var newPasswordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(newPassword);
newPasswordValidationResults.Where(r => !r.Passed)
.Each(r => ModelState.AddModelError("NewPassword", "Please enter a new password."));
if (!ModelState.IsValid)
return View();
if (newPassword != confirmPassword)
ModelState.AddModelError("ConfirmPassword", "The passwords do not match.");
if (!ModelState.IsValid)
return View();
if (!_userMembershipService.ChangePassword(oldPassword, newPassword))
ModelState.AddModelError("_FORM", "Unable to change your password.");
if (!ModelState.IsValid)
return View();
return View("ChangePasswordSuccessful");
}
所有這些在我看來,有一個代碼味道...
if (!ModelState.IsValid)
return View();
我喜歡這個!謝謝 – 2009-10-12 14:11:10