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.
Questions tagged [asp.net-mvc-2-validation]
80 questions
216
votes
4 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
- 10,013
- 17
- 56
- 102
198
votes
10 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,202
- 4
- 27
- 37
30
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…

Null Pointer
- 9,089
- 26
- 73
- 118
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 {…

Kim Tranjan
- 4,521
- 3
- 39
- 38
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"))
{
…

b3n
- 3,805
- 5
- 31
- 46
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,386
- 3
- 22
- 40
9
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.
- 21,477
- 35
- 135
- 214
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…

Josh
- 2,740
- 3
- 27
- 41
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,343
- 5
- 41
- 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,…

Diep-Vriezer
- 61
- 1
- 3
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…

Maxim Zaslavsky
- 17,787
- 30
- 107
- 173
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…

Zaak
- 482
- 11
- 25
4
votes
2 answers
ASP.Net MVC 2 Controller's TryValidate doesn't validate the List<> items within the model
How do you get a model's validation to also validate child objects in a generic list property.
I have a model that I'm trying to validate, this is not what's being posted to the server, but a composite of some information posted, and information…

Tracker1
- 19,103
- 12
- 80
- 106
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
- 18,273
- 15
- 63
- 96
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
- 575
- 2
- 14
- 27