I have a controller named UserManagement. It has two methods: AddUser and EditUser.
AddUser and EditUser is very similar except only the user name validation.
This should be the user name description in AddUser operation:
[DisplayName("User Name")]
[Remote("ValidateUserName", "UserMgr", ErrorMessage = "The user name is already existing.")]
[RegularExpression(Verifier.REG_EXP_USER_NAME, ErrorMessage = Verifier.ERRMSG_REG_EXP_USER_NAME)]
public string UserName { get; set; }
And this should be the description in EditUser operation:
In AddUser operation, I use the Remote validation to ensure the user name is not in used in the server side, while that's unnecessary in EditUser operation.
To accomplish that, I created two models, two views. Of course, it works. But it has too many repeated code. It breaks DRY.
guogangj
Member
4 Points
7 Posts
Help, how to avoid repeated code.
May 04, 2012 04:58 AM|LINK
Hi all,
I have a controller named UserManagement. It has two methods: AddUser and EditUser.
AddUser and EditUser is very similar except only the user name validation.
This should be the user name description in AddUser operation:
[DisplayName("User Name")] [Remote("ValidateUserName", "UserMgr", ErrorMessage = "The user name is already existing.")] [RegularExpression(Verifier.REG_EXP_USER_NAME, ErrorMessage = Verifier.ERRMSG_REG_EXP_USER_NAME)] public string UserName { get; set; }And this should be the description in EditUser operation:
[DisplayName("User Name")] [Required(ErrorMessage = Verifier.ERRMSG_REQUIRED)] [RegularExpression(Verifier.REG_EXP_USER_NAME, ErrorMessage = Verifier.ERRMSG_REG_EXP_USER_NAME)] public string UserName { get; set; }In AddUser operation, I use the Remote validation to ensure the user name is not in used in the server side, while that's unnecessary in EditUser operation.
To accomplish that, I created two models, two views. Of course, it works. But it has too many repeated code. It breaks DRY.
How could I do? Any ideas?
Thanks.