Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to use DataAnnotations StringLength and SubString to remove text

I have a model classes that has a description property with a data annotation attribute of StringLength and length is set to 100 characters. When this property is more than 100 characters and Entity Framework tries to save this property I get the following error.

 [StringLength(100, ErrorMessage = "Description Max Length is 100")]
        public string Description { get; set; }

Error:
"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details"

I'm not sure if this helps in forming a solution, but I'm using Entity Framework CTP5 and Code First.

What I want to do, is if description is more than 100 characters, then remove characters that are more than 100 characters so that description can be stored and no error will be raised.

I believe I should be able to use the DataAnnotation attribute StringLength manually to help me identify the valid length of description and then use SubString to remove any characters over the valid amount.

Does anyone know how to use DataAnnotation in this situation? Or is there another options that is available?


Update I did what BrokenGlass suggested and here my implementation if:

public static class DataAnnotation
{
    public static int? GetMaxLengthFromStringLengthAttribute(Type modelClass, string propertyName)
    {
        int? maxLength = null;
        var attribute = modelClass.GetProperties()
                        .Where(p => p.Name == propertyName)
                        .Single()
                        .GetCustomAttributes(typeof(StringLengthAttribute), true)
                        .Single() as StringLengthAttribute;

        if (attribute != null)
            maxLength = attribute.MaximumLength;

        return maxLength;
    }
}


int? maxLength = DataAnnotation.GetMaxLengthFromStringLengthAttribute(typeof(Car), "Description");

if(maxLength != null && car.Description.Length > maxLength)
    car.Description = car.Description.Substring(0, maxLength.Value);

BarDev

like image 679
Mike Barlow - BarDev Avatar asked Mar 13 '11 05:03

Mike Barlow - BarDev


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

You could always check the attribute value using reflection, though that approach is not the best if you can get around it - it's not pretty:

var attribute = typeof(ModelClass).GetProperties()
                                  .Where(p => p.Name == "Description")
                                  .Single()
                                  .GetCustomAttributes(typeof(StringLengthAttribute), true) 
                                  .Single() as StringLengthAttribute;

Console.WriteLine("Maximum Length: {0}", attribute.MaximumLength);    
like image 70
BrokenGlass Avatar answered Oct 01 '22 13:10

BrokenGlass


Why all the hassle? Why not

private string _description = string.Empty;

[StringLength(100, ErrorMessage = "Description Max Length is 100")]
public string Description 
{  
    get { return _description; }
    set { _description = value.Substring(0,100); };  // or something equivalent
} 
like image 25
Raymond Avatar answered Oct 01 '22 14:10

Raymond