Asp.Net Mvc 6 Core - Validation issue / bug?What are MVP and MVC and what is the difference?Compile Views in ASP.NET MVCHow do you create a dropdownlist from an enum in ASP.NET MVC?ASP.NET Web Site or ASP.NET Web Application?How do you handle multiple submit buttons in ASP.NET MVC Framework?What is the difference between MVC and MVVM?ASP.NET MVC - Set custom IIdentity or IPrincipalFile Upload ASP.NET MVC 3.0Disable Required validation attribute under certain circumstancesNullReferenceException when using Fluent Validation ASP.NET Core
Two monoidal structures and copowering
CREATE opcode: what does it really do?
Go Pregnant or Go Home
How to safely derail a train during transit?
Type int? vs type int
What does "I’d sit this one out, Cap," imply or mean in the context?
Is this version of a gravity generator feasible?
Detecting if an element is found inside a container
What is the opposite of 'gravitas'?
Customer Requests (Sometimes) Drive Me Bonkers!
Fastening aluminum fascia to wooden subfascia
Short story about space worker geeks who zone out by 'listening' to radiation from stars
Pole-zeros of a real-valued causal FIR system
System.debug(JSON.Serialize(o)) Not longer shows full string
Proof of work - lottery approach
What can we do to stop prior company from asking us questions?
Is a stroke of luck acceptable after a series of unfavorable events?
Anatomically Correct Strange Women In Ponds Distributing Swords
Integer addition + constant, is it a group?
Implement the Thanos sorting algorithm
How do I find the solutions of the following equation?
How to run a prison with the smallest amount of guards?
Why escape if the_content isnt?
Trouble understanding the speech of overseas colleagues
Asp.Net Mvc 6 Core - Validation issue / bug?
What are MVP and MVC and what is the difference?Compile Views in ASP.NET MVCHow do you create a dropdownlist from an enum in ASP.NET MVC?ASP.NET Web Site or ASP.NET Web Application?How do you handle multiple submit buttons in ASP.NET MVC Framework?What is the difference between MVC and MVVM?ASP.NET MVC - Set custom IIdentity or IPrincipalFile Upload ASP.NET MVC 3.0Disable Required validation attribute under certain circumstancesNullReferenceException when using Fluent Validation ASP.NET Core
I've migrated an MVC4 app to MVC6 (both .net 4.6.1) and am hitting numerous errors with the inbuilt model validation.
I have a number of complex models that are posted to controllers, and unless I disable validation on each model under configure services, they throw unecessary exceptions relating to properties that are irrelevant to validation, or just hangs after postback without reaching the controller action.
I have added the following line to MVC Configuration for all my affected classes, but I've now got a model that requires validation, so turning it off will cause numerous code changes.
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(TestModel)));
I tried this with a test app and can replicate the issue:
Test Controller:
public IActionResult Index()
return View();
[HttpPost]
public IActionResult Index(TestModel model)
return View();
Test Model (for example)
public class TestModel
[Required]
public string Name get; set;
[Required]
[DataType(DataType.EmailAddress)]
public string Email get; set;
public int NameLength
get
return Name.Length;
Without the validation attributes, the code works fine, but is not validated (obviously).
But when this model is posted, a NullReference exception is thrown by the NameLength property, even though no code references it, the property is read only, and the property it depends on is required. This validation happens before control is returned to the controller.
I've tried disabling this functionality in MvcOptions, but it doesn't have any effect:
options.MaxValidationDepth = null;
options.AllowValidatingTopLevelNodes = false;
options.AllowShortCircuitingValidationWhenNoValidatorsArePresent = true;
I don't know if there's a setting I'm missing, but I would expect the default functionality to ignore properties without validation attributes, or am I doing something wrong?.
Thanks in advance for your help.
Further to @Henks suggestion, I've added the ValidateNever attribute to the readonly properties of one class I was having problems with, which has worked, so the postback reaches the controller now, but its still calling the properties, it just seems to ignore the result:
[ValidateNever]
public Competition PrimaryCompetition
get
return GetCompetition(true);
This still triggers a null reference exception because it relies on another property that is [Required] but is not validated first.
I'm beginning to think this is a bug rather than an error on my part.
c# asp.net model-view-controller core
add a comment |
I've migrated an MVC4 app to MVC6 (both .net 4.6.1) and am hitting numerous errors with the inbuilt model validation.
I have a number of complex models that are posted to controllers, and unless I disable validation on each model under configure services, they throw unecessary exceptions relating to properties that are irrelevant to validation, or just hangs after postback without reaching the controller action.
I have added the following line to MVC Configuration for all my affected classes, but I've now got a model that requires validation, so turning it off will cause numerous code changes.
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(TestModel)));
I tried this with a test app and can replicate the issue:
Test Controller:
public IActionResult Index()
return View();
[HttpPost]
public IActionResult Index(TestModel model)
return View();
Test Model (for example)
public class TestModel
[Required]
public string Name get; set;
[Required]
[DataType(DataType.EmailAddress)]
public string Email get; set;
public int NameLength
get
return Name.Length;
Without the validation attributes, the code works fine, but is not validated (obviously).
But when this model is posted, a NullReference exception is thrown by the NameLength property, even though no code references it, the property is read only, and the property it depends on is required. This validation happens before control is returned to the controller.
I've tried disabling this functionality in MvcOptions, but it doesn't have any effect:
options.MaxValidationDepth = null;
options.AllowValidatingTopLevelNodes = false;
options.AllowShortCircuitingValidationWhenNoValidatorsArePresent = true;
I don't know if there's a setting I'm missing, but I would expect the default functionality to ignore properties without validation attributes, or am I doing something wrong?.
Thanks in advance for your help.
Further to @Henks suggestion, I've added the ValidateNever attribute to the readonly properties of one class I was having problems with, which has worked, so the postback reaches the controller now, but its still calling the properties, it just seems to ignore the result:
[ValidateNever]
public Competition PrimaryCompetition
get
return GetCompetition(true);
This still triggers a null reference exception because it relies on another property that is [Required] but is not validated first.
I'm beginning to think this is a bug rather than an error on my part.
c# asp.net model-view-controller core
Have you seenValidateNeverAttribute?
– Henk Holterman
Mar 7 at 12:50
Is the model coming in correctly on the controller?
– James Morrison
Mar 7 at 13:27
@Henk, thanks for that, I looked for NoValidate or IgnoreValidation didn't see ValidateNever. It works on my test app. I'll try it on my main project, but it seems an odd decision to have to explicitly exclude properties for validation, as well as explicitly include them!.
– Ian Gordon
Mar 7 at 13:47
@James. No it hangs without raising an exception, unless one of the properties on the model throws an exception of course. I'll try Henks suggestion, but its a long winded fix if it works!.
– Ian Gordon
Mar 7 at 13:51
add a comment |
I've migrated an MVC4 app to MVC6 (both .net 4.6.1) and am hitting numerous errors with the inbuilt model validation.
I have a number of complex models that are posted to controllers, and unless I disable validation on each model under configure services, they throw unecessary exceptions relating to properties that are irrelevant to validation, or just hangs after postback without reaching the controller action.
I have added the following line to MVC Configuration for all my affected classes, but I've now got a model that requires validation, so turning it off will cause numerous code changes.
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(TestModel)));
I tried this with a test app and can replicate the issue:
Test Controller:
public IActionResult Index()
return View();
[HttpPost]
public IActionResult Index(TestModel model)
return View();
Test Model (for example)
public class TestModel
[Required]
public string Name get; set;
[Required]
[DataType(DataType.EmailAddress)]
public string Email get; set;
public int NameLength
get
return Name.Length;
Without the validation attributes, the code works fine, but is not validated (obviously).
But when this model is posted, a NullReference exception is thrown by the NameLength property, even though no code references it, the property is read only, and the property it depends on is required. This validation happens before control is returned to the controller.
I've tried disabling this functionality in MvcOptions, but it doesn't have any effect:
options.MaxValidationDepth = null;
options.AllowValidatingTopLevelNodes = false;
options.AllowShortCircuitingValidationWhenNoValidatorsArePresent = true;
I don't know if there's a setting I'm missing, but I would expect the default functionality to ignore properties without validation attributes, or am I doing something wrong?.
Thanks in advance for your help.
Further to @Henks suggestion, I've added the ValidateNever attribute to the readonly properties of one class I was having problems with, which has worked, so the postback reaches the controller now, but its still calling the properties, it just seems to ignore the result:
[ValidateNever]
public Competition PrimaryCompetition
get
return GetCompetition(true);
This still triggers a null reference exception because it relies on another property that is [Required] but is not validated first.
I'm beginning to think this is a bug rather than an error on my part.
c# asp.net model-view-controller core
I've migrated an MVC4 app to MVC6 (both .net 4.6.1) and am hitting numerous errors with the inbuilt model validation.
I have a number of complex models that are posted to controllers, and unless I disable validation on each model under configure services, they throw unecessary exceptions relating to properties that are irrelevant to validation, or just hangs after postback without reaching the controller action.
I have added the following line to MVC Configuration for all my affected classes, but I've now got a model that requires validation, so turning it off will cause numerous code changes.
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(TestModel)));
I tried this with a test app and can replicate the issue:
Test Controller:
public IActionResult Index()
return View();
[HttpPost]
public IActionResult Index(TestModel model)
return View();
Test Model (for example)
public class TestModel
[Required]
public string Name get; set;
[Required]
[DataType(DataType.EmailAddress)]
public string Email get; set;
public int NameLength
get
return Name.Length;
Without the validation attributes, the code works fine, but is not validated (obviously).
But when this model is posted, a NullReference exception is thrown by the NameLength property, even though no code references it, the property is read only, and the property it depends on is required. This validation happens before control is returned to the controller.
I've tried disabling this functionality in MvcOptions, but it doesn't have any effect:
options.MaxValidationDepth = null;
options.AllowValidatingTopLevelNodes = false;
options.AllowShortCircuitingValidationWhenNoValidatorsArePresent = true;
I don't know if there's a setting I'm missing, but I would expect the default functionality to ignore properties without validation attributes, or am I doing something wrong?.
Thanks in advance for your help.
Further to @Henks suggestion, I've added the ValidateNever attribute to the readonly properties of one class I was having problems with, which has worked, so the postback reaches the controller now, but its still calling the properties, it just seems to ignore the result:
[ValidateNever]
public Competition PrimaryCompetition
get
return GetCompetition(true);
This still triggers a null reference exception because it relies on another property that is [Required] but is not validated first.
I'm beginning to think this is a bug rather than an error on my part.
c# asp.net model-view-controller core
c# asp.net model-view-controller core
edited Mar 7 at 15:51
Ian Gordon
asked Mar 7 at 12:18
Ian GordonIan Gordon
92
92
Have you seenValidateNeverAttribute?
– Henk Holterman
Mar 7 at 12:50
Is the model coming in correctly on the controller?
– James Morrison
Mar 7 at 13:27
@Henk, thanks for that, I looked for NoValidate or IgnoreValidation didn't see ValidateNever. It works on my test app. I'll try it on my main project, but it seems an odd decision to have to explicitly exclude properties for validation, as well as explicitly include them!.
– Ian Gordon
Mar 7 at 13:47
@James. No it hangs without raising an exception, unless one of the properties on the model throws an exception of course. I'll try Henks suggestion, but its a long winded fix if it works!.
– Ian Gordon
Mar 7 at 13:51
add a comment |
Have you seenValidateNeverAttribute?
– Henk Holterman
Mar 7 at 12:50
Is the model coming in correctly on the controller?
– James Morrison
Mar 7 at 13:27
@Henk, thanks for that, I looked for NoValidate or IgnoreValidation didn't see ValidateNever. It works on my test app. I'll try it on my main project, but it seems an odd decision to have to explicitly exclude properties for validation, as well as explicitly include them!.
– Ian Gordon
Mar 7 at 13:47
@James. No it hangs without raising an exception, unless one of the properties on the model throws an exception of course. I'll try Henks suggestion, but its a long winded fix if it works!.
– Ian Gordon
Mar 7 at 13:51
Have you seen
ValidateNeverAttribute ?– Henk Holterman
Mar 7 at 12:50
Have you seen
ValidateNeverAttribute ?– Henk Holterman
Mar 7 at 12:50
Is the model coming in correctly on the controller?
– James Morrison
Mar 7 at 13:27
Is the model coming in correctly on the controller?
– James Morrison
Mar 7 at 13:27
@Henk, thanks for that, I looked for NoValidate or IgnoreValidation didn't see ValidateNever. It works on my test app. I'll try it on my main project, but it seems an odd decision to have to explicitly exclude properties for validation, as well as explicitly include them!.
– Ian Gordon
Mar 7 at 13:47
@Henk, thanks for that, I looked for NoValidate or IgnoreValidation didn't see ValidateNever. It works on my test app. I'll try it on my main project, but it seems an odd decision to have to explicitly exclude properties for validation, as well as explicitly include them!.
– Ian Gordon
Mar 7 at 13:47
@James. No it hangs without raising an exception, unless one of the properties on the model throws an exception of course. I'll try Henks suggestion, but its a long winded fix if it works!.
– Ian Gordon
Mar 7 at 13:51
@James. No it hangs without raising an exception, unless one of the properties on the model throws an exception of course. I'll try Henks suggestion, but its a long winded fix if it works!.
– Ian Gordon
Mar 7 at 13:51
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55043621%2fasp-net-mvc-6-core-validation-issue-bug%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55043621%2fasp-net-mvc-6-core-validation-issue-bug%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you seen
ValidateNeverAttribute?– Henk Holterman
Mar 7 at 12:50
Is the model coming in correctly on the controller?
– James Morrison
Mar 7 at 13:27
@Henk, thanks for that, I looked for NoValidate or IgnoreValidation didn't see ValidateNever. It works on my test app. I'll try it on my main project, but it seems an odd decision to have to explicitly exclude properties for validation, as well as explicitly include them!.
– Ian Gordon
Mar 7 at 13:47
@James. No it hangs without raising an exception, unless one of the properties on the model throws an exception of course. I'll try Henks suggestion, but its a long winded fix if it works!.
– Ian Gordon
Mar 7 at 13:51