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
1
vote
2 answers

.NET CORE Invoke ViewComponent from Controller

I am having issues with loading a ViewComponent from a controller, my main view is Contact.cshtml and my ViewComponent is AddContact.cshtml. AddContact is a simple form with a submit button, I am trying to get it so that when the model is invalid,…
1
vote
2 answers

Model attributes to ignore validation of html entities

I want to input html in the database and also display it back as html. I wrote my view model like this: public class TemplateVM { [HiddenInput(DisplayValue = false)] public int TemplateId { get; set; } public string Name { get; set; } …
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
1
vote
0 answers

Is ApiController model validation different than TryValidateObject?

I am using .NET Core 3.1 to develop REST API. This is the controller that I am using (stripped down to basics just to demonstrate the issue, it returns the same data as it…
Sam Carlson
  • 1,891
  • 1
  • 17
  • 44
1
vote
1 answer

Complex model validation

Is there ways to implement model validation with complex rules like "if one radio button was selected it's necessary to select one radio from another radio buttons group. If radio button with additional textbox was selected so it's necessary to fill…
kseen
  • 359
  • 8
  • 56
  • 104
1
vote
2 answers

How to manually deserialize JSON and validate model like Web API does it automatically?

When posting JSON to Web API, not only does it get deserialized automatically, one can also use model validation like // ItemPostRequest class ItemPostRequest { [Required] // this will automatically be validated and errors created if it is…
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
1
vote
0 answers

Net Core 3: Dynamic Model Validation at API runtime

We have 1000 small lookup table editor in database. Each having few rows maximum. Instead of writing 1000 Apis, services, angular proxies we created an Object lookup viewer/editor in grid cell. Small Tables like ProductCodeLookup, AddressType,…
1
vote
1 answer

How to validate an integer in Asp.net to fall into multiple intervals with built-in validation (or a range with exception)?

I have an integer that can fall in range from -1000 to 1000 but cannot be zero. I cannot find any built-in validation that can fix this: Range can be only used once and does not allow exceptions or intervals Required does not allow zero…
1
vote
0 answers

modelvalid: R function for binary Logistic Regression internal validation In GmAMisc: 'Gianmarco Alberti' Miscellaneous

I got this code to perform an internal validation of a binary Logistic Regression model from this -https://rdrr.io/cran/GmAMisc/man/modelvalid.html- and it works perfectly with the dataset suggested data(log_regr_data). Unfortunately when I use my…
Fabrizio
  • 11
  • 1
1
vote
1 answer

ASP.NET Core validation: switch off server validation for some properties dynamically

Is there a way to switch off validation of some model properties based on the bound model value. Example: public class ContactModel { public string Name { get; set; } public string ContactType { get; set; } [Required] public string…
Zoka
  • 2,312
  • 4
  • 23
  • 33
1
vote
0 answers

How to prevent this route error in remote model validation ASP.NET Core

I'm trying to validate email using this remote validation attribute, but I keep getting this error. Does anyone know the exact reason for this? public class StudentVm { public string Id { get; set; } [Required] public string…
1
vote
2 answers

Is there any way to add error code to ModelState error

I'm looking for a way to add error code alongside the error message to ModelState. for example ModelState.AddModelError("ErrorKey", new { Code = 4001, Message = "Some error message" }); For some bad requests client should do an action and comparing…
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
1
vote
1 answer

How to show asp-for-validation error message in a tooltip instead of span

ASP.NET Core has beautiful way of handle valiadtion. But the validation style showing in frontend is very old school. The way i have followed: Custom model annotation with error message: [Required(ErrorMessage = "UserName is Required")] …
1
vote
3 answers

ModelState is invalid for a nullable property

I have a model where the property CompanyID allows a null value public partial class ReportTemplateItem { [Key] public int ReportTemplateItemID { get; set; } [Required] public int ReportTemplateID { get; set; } public int?…
1
vote
3 answers

How to Handle C# Data Type Model Validation before get to controller

I have this request model in C# WebAPI public class RequestModel{ public int Code { set; get; } } then I have API Controller that receive that model as its request body (POST). The problem is, if i run a negative case for testing purpose. I…
hphp
  • 2,142
  • 2
  • 13
  • 26
1
vote
1 answer

.NET Core 3.1 custom model validation with fluentvalidation

Im trying to learn .net 3.1 by building a small test webapi and currently my objective is to validate dtos with fluentvalidation and in case it fails, present a custom json to the caller. The problems i have found and cant get over are two; i cant…
sergio
  • 1,026
  • 2
  • 19
  • 43