Check only dirty inputs

My current web project has an administration interface in which user details can be edited (I’m not really revolutionizing, I admit ...). The view uses a strongly typed user model with validation attributes:

public class UserPrototype
{
    [Required]
    [StringLength(50, MinimumLength = 4)]
    public string Username { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 1)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 1]
    public string LastName { get; set; }

    [Required]
    [StringLength(250, MinimumLength = 6]
    public string Password { get; set; }

    /*
      And so on
    */
}

When the user is updated, I would only like to update those fields in the database that have really changed. The main reason is the password field - the password, of course, is stored as a hash, so when the user returns for editing, there is nothing significant in this field. But validation of model binding requires a valid password.

, , - , ( , javascript)? UserPrototype, Required.

+3
2

, .

public class BaseUserPrototype
{
    [Required]
    [StringLength(50, MinimumLength = 4)]
    public string Username { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 1)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 1]
    public string LastName { get; set; }

    /*
      And so on
    */
}

public class NewUserPrototype: BaseUserPrototype
{
    [Required]
    [StringLength(250, MinimumLength = 6]
    public string Password { get; set; }

    /*
      And so on
    */
}
public class EditUserPrototype: BaseUserPrototype
{
    [StringLength(250, MinimumLength = 6]
    public string Password { get; set; }
    /*
      And so on
    */
}
+1

IDataErrorInfo; :

public class Entity: IDataErrorInfo
{
// this is the dictionary of errors
    private readonly Dictionary<string, string> _errors = new Dictionary<string, string>();
    private string _password;

    public string Password
    {
        get{return _password;}
        set
        {
        // YOUR ACTUAL VALIDATION CODE HERE. IF THERE IS A VALIDATION ERROR - ADD IT TO THE DICTIONARY
            if(string.IsNullOrEmpty(value))
            _errors["Password"] = "Error with password field";
        }
    }
}
0

All Articles