model-validation

RegularExpression validation fails at ModelState.IsValid when input is 0.00

放肆的年华 提交于 2021-02-10 05:14:59
问题 I have a problem similar to this question I'm using MVC . I have added a regular expression in my ViewModel to force the users to enter a number with 1 or 2 decimal places. I am thinking there must be an error in my Regex but the same statement is working fine elsewhere. If I put in 0.00 as my rate , when I submit my form, ModelState.IsValid returns as false . [RegularExpression(@"^(\d+\.\d{1,2})$", ErrorMessage = "Please enter valid rate with 1 or 2 decimal places")] public double

How To Add Custom ModelValidatorProviders To Web API Project?

▼魔方 西西 提交于 2021-02-07 07:39:28
问题 I'm moving some MVC code to Web API and I need to update my custom ModelValidatorProviders. It seems as though my validators can stay much the same only they should inherit the System.Web.Http.Validation namespace. What I can't figure out is how to add the provider to Web API. When using MVC I can just add the following to my global.asax: ModelValidatorProviders.Providers.Add(new CustomModelValidatorProvider()); How do I use the custom provider with Web API? 回答1: This page Configuring ASP.NET

How to validate json request body as valid json in asp.net core

醉酒当歌 提交于 2020-03-16 08:08:31
问题 In asp.net core 2.1, when a controller action is set as: [HttpPost] public JsonResult GetAnswer(SampleModel question) { return Json(question.Answer); } where the SampleModel is defined as: public class SampleModel { [Required] public string Question { get; set; } public string Answer { get; set; } } this is still considered as valid request: { "question": "some question", "question": "some question 2", "answer": "some answer" } In the controller I can see that second question is the value of

How to validate json request body as valid json in asp.net core

孤街醉人 提交于 2020-03-16 08:06:30
问题 In asp.net core 2.1, when a controller action is set as: [HttpPost] public JsonResult GetAnswer(SampleModel question) { return Json(question.Answer); } where the SampleModel is defined as: public class SampleModel { [Required] public string Question { get; set; } public string Answer { get; set; } } this is still considered as valid request: { "question": "some question", "question": "some question 2", "answer": "some answer" } In the controller I can see that second question is the value of

ASP.NET Core Conditional Validation for controls [duplicate]

本小妞迷上赌 提交于 2020-02-23 02:51:26
问题 This question already has answers here : How do I use IValidatableObject? (6 answers) Closed 4 months ago . For example, I have these 3 properties in my view model public class PageViewModel { [Required] public bool? HasControl { get; set; } [Required] public bool? Critical { get; set; } [Required] public string Description { get; set; } } The problem here is that I want to make the properties Critical Description required if HasControl is true or not required if it's false, which is a radio

How to make this ASP.NET Core Model Validation work on client-side?

此生再无相见时 提交于 2020-01-07 05:02:13
问题 In my ASP.NET Core 1.1.1 app the following Model Validation is not working. I think the issue is related to me not properly adding validation scripts in Main View below. Scenario : I click on a button on Main View that calls a partial view . I enter all correct values, in partial view and submit the form (in partial view), the form successfully gets submitted and all the values are correctly entered into SQL server db. I then intentionally enter a string, say, abc into the input box for price

How to perform async ModelState validation with FluentValidation in Web API?

筅森魡賤 提交于 2020-01-01 12:27:11
问题 I setup a web api project to use FluentValidation using the webapi integration package for FluentValidation. Then I created a validator that uses CustomAsync(...) to run queries against the database. The issue is that the validation seems to deadlock when awaiting for the database task. I did some investigation, it seems that the MVC ModelState API is synchronous, and it calls a synchronous Validate(...) method that makes FluentValidation to call task.Result , causing the deadlock. Is it

@Scripts doesn't exist in Asp.Net MVC

删除回忆录丶 提交于 2019-12-25 09:09:27
问题 I'm trying to add Model validation in Asp.Net MVC , So I need to import the libraries at first inside Views\Share_Layout.cshtml . Then it kills me in a second. @Scripts doesn't exist What is going on ? If some further information needed , please tell me. Thanks :) 回答1: Install Microsoft.AspNet.Web.Optimization In web.config (Views folder) make sure you have under namespaces <add namespace="System.Web.Optimization"/> 来源: https://stackoverflow.com/questions/43159078/scripts-doesnt-exist-in-asp