3

I have an mvc razor page which contains a form with fields created dynamically from the database ie the field name, type (textbox etc) are set in the db table as well as StringLength

Is there some way in my model to set the StringLength on the field to be a value from one of the other properties?

The questions are built up using an editor template for the QuestionModel, these are the relevant fields in the QuestionModel;

public string QuestionTypeName { get; set; }  --> ie this is like TextBox, TextArea etc and is used to create the write EditorTemplate

[RequiredIf("Required", true)]
[StringLength(<<this is where I want to use the property FieldLength>>)]
public string Answer { get; set; }

[DisplayName("Question name")]
public string Question { get; set; }

public bool Required { get; set; }
public Int32 FieldLength { get; set; }
tereško
  • 58,060
  • 25
  • 98
  • 150
MartinaL
  • 151
  • 1
  • 3
  • 12
  • possible duplicate of [ASP.NET-MVC 2 DataAnnotations StringLength](http://stackoverflow.com/questions/2489766/asp-net-mvc-2-dataannotations-stringlength) – Ramesh Rajendran Oct 21 '13 at 04:41
  • HI Ramesh, I have read this other port that you have mentioned and it did not help me solve my issue which is why I created this post – MartinaL Oct 21 '13 at 06:08

2 Answers2

2

You can write your own Custom Validation Attribute as follows

Implementation

    public class CustomValidation : ValidationAttribute
    {
          public CustomValidation(string otherproperty)
          : base("Your Error Message goes here")
          {
              OtherProperty = otherproperty;
          }

          public string OtherProperty { get; set; }

          public override string FormatErrorMessage(string name)
          {
               return string.Format(ErrorMessageString, name, OtherProperty);
          }

          protected override ValidationResult IsValid(object firstobject, ValidationContext validationContext)
          {
                var firstValue = firstobject;
                var secondValue = GetSecondObject(validationContext);

          if(firstValue !=null && secondValue!=null)
          {
                if (//Your Condition for validation failure)
                {
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
                }               
          }
                return ValidationResult.Success;
          }

         protected object GetSecondObject(
ValidationContext validationContext)
         {
                var propertyInfo = validationContext
                              .ObjectType
                              .GetProperty(OtherProperty);
               if (propertyInfo != null)
               {
                    var secondValue = propertyInfo.GetValue(
                    validationContext.ObjectInstance, null);
                    return secondValue as object;
               }
              return null;
        }
  }

Usage:

public class ClassName
{
    [CustomValidation("FieldLength")] // pass the property name 
    public string Answer { get; set; }
    .....
Jatin patil
  • 4,252
  • 1
  • 18
  • 27
0

Try this

[StringLength(50, MinimumLength=10)]
public string Answer { get; set; }

or use MaximumLength and see this link for String Length Attribute

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • How is this helping me set the max length to a dynamic value from the database? you are just setting it to 50? – MartinaL Oct 21 '13 at 06:07