Questions tagged [asp.net-mvc-2-validation]

The ASP.NET MVC 2 modelstate validation checks if a model is valid. You can define a correct model state and check if the users input matches that validation.

80 questions
198
votes
3 answers

ModelState.AddModelError - How can I add an error that isn't for a property?

I am checking my database in Create(FooViewModel fvm){...} to see if the fvm.prop1 and fvm.prop2 already exist in that combination; if so, I want to add an error to the modelstate, then return the whole view. I tried: public ActionResult…
Scott Baker
  • 8,951
  • 14
  • 46
  • 83
195
votes
9 answers

ASP.NET MVC Html.ValidationSummary(true) does not display model errors

I have some problem with Html.ValidationSummary. I don't want to display property errors in ValidationSummary. And when I set Html.ValidationSummary(true) it does not display error messages from ModelState. When there is some Exception in controller…
msi
  • 3,092
  • 4
  • 24
  • 35
29
votes
9 answers

Redirect to an action from Application_BeginRequest in global.asax

In my web application I am validating the url from glabal.asax . I want to validate the url and need to redirect to an action if needed. I am using Application_BeginRequest to catch the request event. protected void…
19
votes
3 answers

Default resource for data annotations in ASP.NET MVC

There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name {…
15
votes
6 answers

How to display MVC 3 client side validation results in validation summary

I have a registration form on which I use client side validation (Required, StringLength etc. specified on my view model). The form is currently pretty much how the scaffolder creates it: @using (Html.BeginForm("Index", "Registration")) { …
14
votes
2 answers

Validation best practice for Model and ViewModel

I have separate model and viewmodel classes. Where viewmodel classes only do UI level validation (refer: Validation: Model or ViewModel). I can verify on post action in a controller that model (vewmodel) is valid. The ask: How do I validate the…
Yahya
  • 3,078
  • 2
  • 18
  • 39
8
votes
4 answers

MVC2 Client-side validation for a DateTime?

What approach do you recommend for validating a DateTime on the client side in MVC? Let's say I have a model with a property named DateOfBirth that is a DateTime, like so. public class UserModel { [DataType(DataType.Date)] public DateTime…
8
votes
3 answers

ASPNET MVC - Why is ModelState.IsValid false "The x field is required" when that field does have a value?

I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string OrderNumber {get;set;} // etc. } When I submit an order from the view (using $.post, not input type=submit) it goes to my…
JK.
  • 20,010
  • 29
  • 124
  • 204
7
votes
5 answers

ASP.MVC 2 RTM + ModelState Error at Id property

I have this classes: public class GroupMetadata { [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Required] public string Name { get; set; } } [MetadataType(typeof(GrupoMetadata))] public partial class Group { …
Zote
  • 5,255
  • 5
  • 39
  • 43
6
votes
1 answer

Support for nested model and class validation with ASP.NET MVC 2.0

I'm trying to validate a model containing other objects with validation rules using the System.ComponentModel.DataAnnotations attributes was hoping the default MVC implementation would suffice: var obj = js.Deserialize(json,…
5
votes
2 answers

ASP.NET MVC 2 and ComponentModel.DataAnnotations Validation: minimum value attribute

I'm decorated a ViewModel in my ASP.NET MVC 2 site with System.ComponentModel.DataAnnotations validation attributes. For one of my fields, named Price, I want to validate that the value is not below some extent, in this case 0. I know that…
4
votes
2 answers

custom validation attribute not working on client, just on server

I am trying to implement custom attribute validation, similar to one demonstrated here in ScottGu's blog: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx I have this custom validator attribute for Email: public…
4
votes
1 answer

Validation based on other field?

In ASP.NET MVC 2, I have a Linq to sql class that contains a series of fields. Now I one of the fields is required when another field has a certain (enum) value. I've come so far that I wrote a custom validation attribute, which can take an enum as…
jao
  • 17,118
  • 13
  • 58
  • 92
3
votes
4 answers

html.textboxfor() should be exactly 7 characters

I need a text box which should be exactly of 7 characters, for which I have written Html.TextboxFor(x=>x.Number, new {maxLength = "7"}; This case takes me only 7 characters , but if I want to take less than 7, it is taking? Is there any property…
michael
  • 525
  • 2
  • 13
  • 27
3
votes
5 answers

What's the recommended place to perform validation: ViewModel, Model or Controller?

I have a registration page and would like to perform some validation (in addition to the StringLength and Required annotations on my ViewModel) for duplicate usernames and email addresses. Currently I perform this validation in my controller when…
b3n
  • 3,595
  • 5
  • 27
  • 46
1
2 3 4 5 6