c# - ASP.NET MVC Model validation ignoring inputs that have become readonly -


i have form shipping , billing information using view model containing required attributes.

here's exctract of code enough explain issue.

extract of view model:

public class checkoutviewmodel {     [required]     public string shippingpostalcode { get; set; }      [required(errormessage = "*")]     public string billingpostalcode { get; set; } } 

extract of razor form:

@html.textboxfor(m => m.shippingpostalcode, new { @class = "form-control", placeholder = "postal code" }) @html.textboxfor(m => m.billingpostalcode, new { @class = "form-control", placeholder = "postal code", data_bind = "attr: { 'readonly': billingsameasshipping }" }) @html.validationmessagefor(model => model.billingpostalcode) 

i use knockout make billing fields (including billingpostalcode) readonly if checkbox checked using observable billingsameasshipping variable.

<input type="checkbox" data-bind="checked: billingsameasshipping" id="billingsameasshippingcheckbox" /> 

if checkbox not checked, , billingpostalcode left empty, validation fires correctly. however, if billingpostalcode empty checkbox checked, making field readonly, validation not fired , modelstate's isvalid having value true.

any clues if expected behaviour, or ideas how work around it?

any appreciated. thanks.

edit: added javascript code if helps.

var shipping = {     billingsameasshipping: ko.observable(false) }; ko.applybindings(shipping);  $("#billingsameasshippingcheckbox").on("change", function () {     if (this.checked) {         $("#billingpostalcode").val($("#shippingpostalcode").val());     } }); 

it seems jquery validator uses ignores readonly inputs. managed fix this.

$.validator.setdefaults({ ignore: null }); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -