【问题标题】:modelstate validation in asp.net mvc 2.0asp.net mvc 2.0 中的模型状态验证
【发布时间】:2010-10-29 10:39:43
【问题描述】:

我已经通过使用个人资料提供者扩展会员提供者来实现自定义注册页面。我成功注册了用户。现在我想验证注册页面的字段。内置注册页面具有内置验证消息。 在我的编码中,我没有将模型传递给注册操作,而是传递属性。因此,如果我使用 If(ModelState.IsValid),即使我没有填写任何字段,它也始终为真。但是在它引发异常之后,但是未在页面中显示错误消息。请告诉我我必须做什么。我如何获得验证消息。

我看到 Account Models 类用于注册模型,内置验证条件。所以我也在为我的属性写这样的内容。

提前致谢,

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

【问题讨论】:

    标签: asp.net asp.net-mvc-2


    【解决方案1】:

    ModelState 是有效的,因为有一个空白字段并不是无效的。

    您必须在操作中手动检查每个字段 (if (FirstName == null) ModelState.AddModelError("blabla");)

    或者(我建议)您创建一个 ViewModel 并为其提供验证属性

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

    【讨论】:

    • 嗨法比诺,我照你说的做了,但它仍然给出了真实的结果,并且在给出如下错误之后,
    • public MembershipCreateStatus CreateUser(string userName, string password, string email) { if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");我是否也必须编写验证消息
    • 那是因为 userName 为空,不是吗?如果您使用具有 [Required] 属性的视图模型,则不应发生这种情况
    • 命名空间 Stalbans.Models { public class UserProfile : ProfileBase { [Required] public virtual string FirstName { get { return ((string)(this.GetPropertyValue("FirstName"))); } 设置 { this.SetPropertyValue("FirstName", value); } } 我对所有属性都这样填充,但我仍然遇到问题。
    • 和 public ActionResult UserRegistration(UserProfile userProfile) ?
    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 2013-08-07
    • 1970-01-01
    • 2010-12-07
    • 2015-02-26
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多