2010-05-30 157 views
0

我目前使用DataAnnotations来验证我的MVC 2应用程序。但是,我遇到了一个小问题。模型中的MVC验证

我目前有一个类型为User的对象,它具有许多属性。所有这些都是必需的。

public class User 
    { 

     [Required(ErrorMessage = "Username is required")] 
     public string Username { get; set; } 

     [Required(ErrorMessage = "Password is required")] 
     public string Password { get; set; } 

     [Required(ErrorMessage = "Email is required")] 
     public string Email { get; set; } 

     [Required(ErrorMessage = "First name is required")] 
     public string Firstname { get; set; } 

     [Required(ErrorMessage = "Last name is required")] 
     public string Lastname { get; set; } 


    } 

注册时,这些都使用模型绑定器映射,一切都很好。但是,在“编辑我的详细信息”页面上,只有名字,姓氏和电子邮件可以更新。 每当查看帖子回来和modelbinding应用我得到一个警报用户名/密码是必填字段。尽管在这一点上它不是必需的。我想过两种方法来解决这个问题,我觉得这两种方法都不适合(但可能是错误的)

1:创建一个自定义视图模型。这样可以正常工作,但数据注释需要应用于此视图模型,这意味着对模型和用户对象进行重复验证。

2:将所有字段包含在渲染视图中并将其发回。这具有安全风险,看起来非常混乱,并且不能很好地适应复杂的视图模型。

任何人都可以推荐这种情况的最佳做法吗?

回答

1

最近有类似问题: Needing to copy properties before validation。作为回应,我建议创建自定义ModelBinder仅用于此特定操作,我仍然认为这是最佳解决方案。

+0

感谢您的链接@tpeczek。你的解决方案是一个很好的选择,但是我已经决定在阅读Jimmy Bogard在http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/上发表的文章后,去1:1 View:ViewModel设置。 how-we-do-mvc-view-models.aspx。 – WDuffy 2010-05-30 20:10:45

0
DataType 

Specify the datatype of a property 
DisplayName 

specify the display name for a property. 
DisplayFormat 

specify the display format for a property like different format for Date proerty. 
Required 

Specify a property as required. 
ReqularExpression 

validate the value of a property by specified regular expression pattern. 
Range 

validate the value of a property with in a specified range of values. 
StringLength 

specify min and max length for a string property. 
MaxLength 

specify max length for a string property. 
Bind 

specify fields to include or exclude when adding parameter or form values to model properties. 
ScaffoldColumn 

specify fields for hiding from editor forms. 

using System.ComponentModel; 
using System.ComponentModel.DataAnnotations; 
using System.Web.Mvc; 
namespace Employee.Models 
{ 
[Bind(Exclude = "EmpId")] 
public class Employee 
{ 
[ScaffoldColumn(false)] 
public int EmpId { get; set; } 
[DisplayName("Employee Name")] 
[Required(ErrorMessage = "Employee Name is required")] 
[StringLength(100,MinimumLength=3)] 
public String EmpName { get; set; } 
[Required(ErrorMessage = "Employee Address is required")] 
[StringLength(300)] 
public string Address { get; set; } 
[Required(ErrorMessage = "Salary is required")] 
[Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")] 
public int Salary{ get; set; } 
[Required(ErrorMessage = "Please enter your email address")] 
[DataType(DataType.EmailAddress)] 
[Display(Name = "Email address")] 
[MaxLength(50)] 
[RegularExpression(@"[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")] 
public string Email { get; set; } 
} 
} 
+0

像这样的仅有代码的答案对社区没有帮助。注意详细说明*如何回答原来的问题? – Werner 2014-03-13 20:13:32