MVC Validation in Model

I'm currently using DataAnnotations to validate my MVC 2 app. However, I've ran into a small problem.

I currently have an object of type User which has a number of properties. All of which are required.

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; }


    }

At signup, these are all mapped using the modelbinder and everything works great. However, on the "edit my detail" page only Firstname, Lastname and Email can be updated. Whenever the view posts back and modelbinding is applied I'm getting an alert Username/Password is a required field. Even though it is not required at this point. I've thought of two ways to get around this neither of which I feel are suitable (but may be wrong)

1: Create a custom viewmodel. This will work fine, but data annotations will need to be applied to this viewmodel meaning duplicate validation on the model and the user object.

2: Include all the fields in the renderd view and post them back. This has security risks, seems really messy and wouldn't scale well to complex viewmodels.

Can anyone recommend a best practice for this situation?


There was similar question recently: Needing to copy properties before validation. In response I have suggested creating custom ModelBinder for usage only in this particular action, and I still believe it's a best solution.


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._%+-]+@[a-z0-9.-]+.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
}
}
链接地址: http://www.djcxy.com/p/56598.html

上一篇: Specflow与MVC模型验证问题

下一篇: 模型中的MVC验证