c# - MVC Range Attribute fails when a hidden field is added to the model -


I have a basic model that includes assets used by a sub-model to pre-fill a field on the scene. I would like to move the property into a sub model, where it is, but when I do this, the range feature of another property fails.

  Parent Submodel Submodel {public submodel submodel} {/ p>  

received; set;}} public class SubModel {public UIT} default Price {Receive; Set;} [Required] [Range (1,100)] Public Utd Color {Received; Set;} Public Hair EnableRange {see; Set;}}

Template) looks like

  @modelSomModel @HTMLHindFor (model => models. Default value) @html.txt TABAX (model = & gt; model. Category ID) & lt; script & gt; $ ('# EnableRange') .changes (function () {if ($ ('# EnableRange'). Val ()) {// Remove disabled attribute from RangeId field} else {// Add disabled attribute to Rangefield field}}  

looks like a controller

  create public functioning (TV visual model model) {try {if (ModelState.IsValid) {// Do Something Meanfulful} // Redisplay the view}}  

with the defaultValue property in SubModel, range of ranges Attribution comes when the category arrives D is unable to form. ModelState.Is valid is valid. When I transfer the DefaultValue property to parent modell, the RangeID is no longer the range feature (because the field is disabled). Which is true due to ModelState.IsValid, because RangeId is never evaluated Verification.

Whatever you think is happening is not happening. Server side model. Nothing is concerned about ISIIDAD, nor is it directly affected by disabling control on the client side (though it can be indirectly affected as we will see below). If nested form fields are posted and there are essential properties of nested objects, verification will always occur.

More likely, the real issue here is that when you divest into the original model, then when you model the parents, the model binder creates an example of SubModel because it is for DefaultValue There is a value. When you take it to parent, and you disable the range, there is no cost to post and therefore no submaldel is made, and there is no verification in this way.

Ie, my guess is that when you move the default value for parents, the subdomain is zero on postback, because there is no instance of validation, something to fail to validate Not even that, especially since you are not creating the EnableRange value

So you really have many problems first, disabling control on the client will not disable verification on the server. Secondly, if the nested form fields are not posted on the server, then no nested object will be created and there will be no validity (in this regard, if you are very careful, validity can be disabled as a side-effect is). Third, if you post some nested form fields but others do not, then osted objects will be created after verification and verification will be done on those fields which were not posted because they were disabled.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -