how to turn on/off the validation when the element is getting disabled/enabled
I want to disable the textbox if the checkbox is unchecked. View:
@Html.CheckBoxFor(model => model.prop1, new { onchange = "OnChange(this,'prop2')" })
...
@Html.TextBoxFor(model => model.prop2)
JavaScript handler
function OnChange(cb, id)
{
if ($(开发者_如何学Pythoncb).is(':checked') == false)
{
$('input#' + id).val('');
$('input#' + id).attr('disabled', 'disabled');
}else {
$('input#' + id).attr('disabled', '');
$('input#' + id).change();
}
}
But there is a problem with validation messages. If prop2 is for example marked as "[Required]" when the textbox is disabled error message should disappear. And when the user check the checkbox I should enable the textbox field and turn on the validation
What should I do for this?
Please check following questions:
Asp.net MVC 3 Conditional validation with DataAnnotations
MVC custom validation: compare two dates
精彩评论