Questions tagged [model-validation]

Model Validation is ASP.NET MVC validation method for your models with property attributes. Model validation works for client and server side validation.

For further information you can check Scott Gu's blog post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

There is a expansion called "DATA ANNOTATIONS EXTENSIONS" for other custom validation types such as CreditCard, min-max integer value etc. http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx

325 questions
88
votes
7 answers

ViewModel validation for a List

I have the following viewmodel definition public class AccessRequestViewModel { public Request Request { get; private set; } public SelectList Buildings { get; private set; } public List Persons { get; private set; } } So in my…
ryan
  • 6,541
  • 5
  • 43
  • 68
87
votes
11 answers

MaxLength Attribute not generating client-side validation attributes

I have a curious problem with ASP.NET MVC3 client-side validation. I have the following class: public class Instrument : BaseObject { public int Id { get; set; } [Required(ErrorMessage = "Name is required.")] [MaxLength(40, ErrorMessage…
Matt Jenkins
  • 2,824
  • 1
  • 30
  • 34
55
votes
8 answers

Array must contain 1 element

I have the following class: public class CreateJob { [Required] public int JobTypeId { get; set; } public string RequestedBy { get; set; } public JobTask[] TaskDescriptions { get; set; } } I'd like to have a data annotation above…
CallumVass
  • 11,288
  • 26
  • 84
  • 154
41
votes
8 answers

MVC .Net Core Model Validation - The value '' is invalid. Error

I am trying to use Model Validation in MVC .Net Core and can't manage to replace this default error message 'The value '' is invalid'. In theory, we can replace our own custom error message by using ErrorMessage Annotation in the Model. But I…
TTCG
  • 8,805
  • 31
  • 93
  • 141
35
votes
5 answers

Model state validation in unit tests

I am writing a unit test for a controller like this: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } the model looks like: public class…
33
votes
9 answers

With HTML5 url input validation assume url starts with http://

HTML5 provides for automatic URL validation :-
This will fail validation for URL's that don't have a protocol prefix - e.g. stackoverflow.com will fail while http://stackoverflow.com will…
Ryan
  • 23,871
  • 24
  • 86
  • 132
23
votes
3 answers

How to do Integer model validation in asp.net mvc 2

I have a registration form and the user must enter the square footage of their house. I would like this value to be only an integer. Is there a way to validate this value using attributes asp.net mvc?
Luke101
  • 63,072
  • 85
  • 231
  • 359
23
votes
3 answers

MVC model validation for date

Is there any default validation for MVC 5 where I can set min and max value of date? In my model i want date validation public class MyClass { [Required(ErrorMessage="Start date and time cannot be empty")] …
Cybercop
  • 8,475
  • 21
  • 75
  • 135
21
votes
4 answers

Model Validation / ASP.NET MVC 3 - Conditional Required Attribute

I'm having trouble with my ASP.NET MVC 3 application. I have 2 propertiesin my model whereby I only want 1 of them required in my view based on whichever one is empty. So for example, if I enter a phone number then email is no longer required and…
CallumVass
  • 11,288
  • 26
  • 84
  • 154
19
votes
4 answers

Can I get a blob by using its url?

I am storing the Azure blob url into my database.Can I get the blob by using that url? Actually I need to update the blob and while doing that I need validations. So I need to convert that database entity model to my local model and apply the…
Bharat Bhushan
  • 1,315
  • 3
  • 18
  • 24
15
votes
3 answers

Best approach for complex model/submodel validation (MVC)

Problem I know there is a lot of ways of doing Model validation within MVC, and there is quite a lot of documentation regarding this topic. However, I'm not quite sure what's the best approach for validating properties of the Model which are "Sub…
9
votes
1 answer

How to handle ASP.NET MVC3 Request Validation exceptions as model or property validation errors

My question is hopefully pretty straight forward. Upon submitting my form, I would like to flag all properties (fields) that have invalid characters (specifically HTML) as model errors. The issue I am facing is that Request Validation kicks in…
zam6ak
  • 7,229
  • 11
  • 46
  • 84
8
votes
2 answers

ASPNET Core 3.0 Model validation error with Inheritance

I just moved to ASP.NET Core 3.0 and it seems that either model validation with inheritance is broken, or I am missing something. When I post the following model with IlVal00 = null, it says: title=One or more validation errors occurred.…
8
votes
3 answers

asp.net core 2.0 model validation not validating data

I am using ASP.NET Core 2.0 default model validation and it seems not working with the ModelState.IsValid always true despite having wrong data in model. [HttpPost("abc")] public async Task Abc([FromBody]AbcViewModel model) { if…
Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64
8
votes
8 answers

How to Increase the size of TextArea in html razor view mvc4?

I have one text area field called Description in my view. I wish to increase the size of that field. My code
* @Html.LabelFor(model => model.Description, new {…
Susan
  • 421
  • 2
  • 6
  • 21
1
2 3
21 22