0

i have a View Model

    [CustomValidation(typeof(MyValidation), "MyMethod")]
    [Serializable()]
    public class TransactionViewModel
   {
      public string InvoiceNumber;
   }

 public class MyValidation
{

  public static ValidationResult validatelength(TransactionViewModel length)
{

bool isValid;
 if (length.InvoiceNumber.Length >15)
isValid = false;
 else
isValid = true;
   if (isValid)
 {
    return ValidationResult.Success;
}
 else
 {
    return new ValidationResult(
      "The Field value is greater than 15");
 }
  }
}

now i am checking some fields of my class object if Validation fails i am checking the model state in controller and return the View ,Added Validation Message for the Invoice Number But still am not getting the Errors

Can we Apply validation Attribute to Model View ,PLS provide the the Solution if i am doing anything wrong

Nitin Bourai
  • 430
  • 7
  • 20

2 Answers2

1

Use

<%= this.Html.ValidationSummary() %>

or

@this.Html.ValidationSummary()

and you'll get what you're looking for.

If your modelstate is not valid, you'll get the error you're looking for.

Your problem is that your error was that your error wasn't associated to any member of the class. In the modelstate it has the key "" because it wasn't associated with any field.

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • +1, how'd you get around that? I.E. making sure the IsValid is associated with the validation field? Am I just missing a constructor? – Malkin Nov 06 '13 at 22:18
  • 1
    I don't really understand what you mean. If you want to validate a field, use ModelState.IsValidField("field") – Fabio Milheiro Nov 12 '13 at 18:43
  • I was just curious as to why the `@this.Html.ValidationSummary()` shows a model error where as `@Html.ValidationSummary()` doesn't? – Malkin Nov 14 '13 at 00:17
  • Are you serious or being sarcastic? The this keyword is an old habit I got from usin fxcop and stylecop. The reason it forces the use of this for instances is that it allows other developers to immediately recognize if the member is static or from an instance. An argument could be made about not using this in views because everyone knows Html is an instance member though. Obviously both work because they are the same. And that is not the same question as before. – Fabio Milheiro Nov 14 '13 at 07:57
0

Maybe you forget to place validationsummary on your view?

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

Please, submit your view for review. Meanwhile here you are the good examples where it works: Validation with the Data Annotation Validators (C#)

Model validation from Scott's blog

Look at this tutorial here handling model error, but use an empty string for the key:

ModelState.AddModelError(string.Empty, "There is something wrong with Foo.");

The error message will present itself in the <%: Html.ValidationSummary() %> as you'd expect.

Yusubov
  • 5,815
  • 9
  • 32
  • 69
  • i am using Validation Summary on the View and everthing is working fine,The Model is not getting Updated but i am not getting the Validation Message, i am getting the View Correctly – Nitin Bourai Jun 18 '12 at 04:39
  • Probably, you are handling it in a different way. Are you using the ModelState.AddModelError(string key, string errorMessage) ? Also look at some extra info in my answer. – Yusubov Jun 18 '12 at 13:56