开发者

server side validation using data annotation is not workingin asp.net mvc 2.0

i have used customised registration page in my asp.net mvc 2.0 application.For that i have used a view Model calss called UserProfile.cs inherited from Profile Base class. Now i want to apply the server side validation to the UserProfile class.But it is not working. My User Profile class is as follows,

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(UserProfile))]
    public class UserProfile :开发者_如何学Python ProfileBase
    {
        [Required]
        public virtual string FirstName
        {
            get
            {
                return ((string)(this.GetPropertyValue("FirstName")));
            }
            set
            {
                this.SetPropertyValue("FirstName", value);
            }
        }     
        [Required]
        public virtual string LastName
        {
            get
            {
                return ((string)(this.GetPropertyValue("LastName")));
            }
            set
            {
                this.SetPropertyValue("LastName", value);
            }
        }


        [Required]
        public virtual string LoginId
        {
            get
            {
                return ((string)(this.GetPropertyValue("LoginId")));
            }
            set
            {
                this.SetPropertyValue("LoginId", value);
            }
        }
}

I have added the System.ComponentModel.DataAnnotation reference.still it is not working .Please tell me why it is not working.


To add the data annotations to an existing model you can do is as follows:

[MetadataType(typeof(User_Validation))]
public partial class User
{
}

public class User_Validation
{
    [Required, StringLength(50)]
    public object FirstName { get; set; }

    [Required, StringLength(50)]
    public object LastName { get; set; }
}

In my case my original model is generated which is why I'm adding my validation using the MetadataTypeAttribute as shown.

My generated object would look something like:

public partial class User
{
    public virtual string FirstName
    {
        get;
        set;
    }

    public virtual string LastName
    {
        get;
        set;
    }
}

You can see that when using the extra class through medadata type that you need to add the properties as object and with the same name.

If you are not trying to add the validation to an existing model in this way then you don't need the MetadataTypeAttribute.


check out this question and asnwers -

validation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜