[英]MVC best practice for view validation?
What is considered best practice for the following example: 以下示例被认为是最佳实践:
Are these 3 separate views? 这是3个单独的视图吗? Or the same view with multiple route options?
还是具有多个路线选项的同一视图?
ActionResult ChangePassword()
ActionResult ChangePassword(string token)
{
// if bad token show this error view partial? set viewdata item and let cshtml decide what to show?
}
Or is a situation like this not really all that logical? 还是这样的情况不是真的那么合乎逻辑吗?
I would separate it into 2 pages: 我将其分为2页:
Change Password 更改密码
Reset Password 重设密码
These two pages seem to operate differently enough that I think you would be better off using separate views, actions, & view models. 这两页的操作方式似乎完全不同,我认为最好使用单独的视图,操作和视图模型。 If you get to the end though, there is nothing wrong with refactoring to remove duplication, but I would predict they will not be as similar as you anticipate.
如果最后,重构消除重复没有什么问题,但是我预计它们不会像您期望的那样相似。
I think you could get away with a single View, you just need to build a robust enough ViewModel: 我认为您可以只使用一个View,只需要构建足够强大的ViewModel:
public class ChangePasswordViewModel(){
public string OldPasswordHash {get; set;} //Remember never to store password in clear text
public string NewPassword{ get; set; }
public string RecoveryToken { get; set; }
}
Based on these properties, you should have all you need to flesh out the presentation logic in your View to cover off all the use cases you describe above. 基于这些属性,您应该拥有充实视图中表示逻辑的所有内容,以掩盖您上面描述的所有用例。 Let me know if you need further guidance.
让我知道您是否需要进一步的指导。
我认为对ASP.NET MVC
的最佳验证是您要Validation Attributes
上的“ Validation Attributes
”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.