1

Currently I have a DataModel object which contains my linq to sql classes(a dmbl file). Currently I use a partial class to validate the incoming input. For example

public partial class User : IEntity
{

    public NameValueCollection CheckModel()
    {
        return GetRuleViolations();
    }

    /// <summary>
    /// Method validates incoming data, by given rules in the if statement.
    /// </summary>
    /// <returns>NameValueCollection</returns>
    private NameValueCollection GetRuleViolations()
    {
        NameValueCollection errors = new NameValueCollection();
        if (string.IsNullOrEmpty(Username))
            errors.Add("Username", "A username is required");
        // and so on
        return errors;
    }

}

Now what I want to try to do is add validation attributes to the fields. For example I want to try to add the required attribute to the field Username instead/in addtion of using the validation I currently have. My question is how can I achieve this because the dmbl file is auto generated. Or maybe it is not possible and should I use a different approach?

1
  • You will have to generate your own L2S classes. Look for T4 templates. Commented Mar 23, 2010 at 11:04

1 Answer 1

2

You should read about Metadata classes. This is example blog entry about it.

Adding Required atrribute to User class will be something like:

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

public class UserMetadata
{
    [Required]
    public string Username { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You might also find the following links interesting: weblogs.asp.net/scottgu/archive/2010/01/15/… and msdn.microsoft.com/en-us/library/ee256141%28VS.100%29.aspx DataAnnotations is the keyword in this situations. ASP.Net MVC 2 even supports client side validation for this kind of validations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.