views:

20

answers:

1

Hi Friends,I have implemented customised registratio page by extending the membership provider using profile provider.I successfully registered the user .Now i want to validate the fields of registration page.Built-in Registration page has builtin validation messages. Bu in my coding i am not passing model to the registration action, instead i am passing properties.So if i Use If(ModelState.IsValid) it is always gives true even i am not filling any fields .but after it throws an exception but not displaying error messages in the page.Please tell me what i have to do.How i am getting my validation messages.

I saw Account Models class in that for register Model built in validation conditions are there.So i am also writing like that for my properties.

Thanks in advance,

public ActionResult UserRegistration(string FirstName, string LastName, string LoginId, string EmailId, string Password, string ConfirmPassword) {

        //int id= int.Parse(ViewData["id"] as string);


            string firstName = FirstName;
            string lastName = LastName;
            string userName = LoginId;
            string email = EmailId;
            string password = Password;
            string confirmPassword = ConfirmPassword;
            if (ModelState.IsValid)
            {
            MembershipCreateStatus status = MembershipService.CreateUser(userName, password, email);
            //MembershipCreateStatus user = Membership.CreateUser(userName, password, email);
            Roles.AddUserToRole(userName, "User");
            UserProfile.NewUser.Initialize(userName, true);
            UserProfile.NewUser.FirstName = firstName;
            UserProfile.NewUser.LastName = lastName;
            if (status == MembershipCreateStatus.Success)
            {
                UserProfile.NewUser.Save();

                FormsService.SignIn(userName, false /* createPersistentCookie */);
                return RedirectToAction("CreateAccountConfirmation");
            }
            else
            {
                ModelState.AddModelError("", AccountValidation.ErrorCodeToString(status));
            }
A: 

the ModelState is valid because it's not invalid to have a blank field.

You either have to check every field manually in your action (if (FirstName == null) ModelState.AddModelError("blabla");)

or (and that I would suggest) you create a ViewModel and provide it with validation attributes

public class RegistrationModel
{
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string LoginId { get; set; }
        [Required]
        public string EmailId { get; set; }
        [Required]
        public string Password { get; set; }
        [Required]
        public string ConfirmPassword { get; set; }
}
Fabiano
Hi Fabino,i did as you told but still it gives true and after it gives error like as follows,
Mallikarjuna
public MembershipCreateStatus CreateUser(string userName, string password, string email) { if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");i have to write the validation messages also or not
Mallikarjuna
that's because userName is null, isn't it? If you use a view model with the [Required] attribute this should not happen
Fabiano
this is my view model
Mallikarjuna
namespace Stalbans.Models{ public class UserProfile : ProfileBase { [Required] public virtual string FirstName { get { return ((string)(this.GetPropertyValue("FirstName"))); } set { this.SetPropertyValue("FirstName", value); } } i fill like this to all properties ,but still i am getting problem.
Mallikarjuna
and public ActionResult UserRegistration(UserProfile userProfile) ?
Fabiano