问题
Here's a sample of what I'm trying to achieve:
@Html.EditorFor(m => m.Description,
new { htmlAttributes =
new
{
@class = "form-control",
@readonly = Model.IsReadOnly,
disabled = Model.IsDisabled
}
})
The problem is that the browser treats the existence of the readonly
and disabled
tokens without checking for their content, so when the IsReadOnly
and IsDisabled
properties are false
, it will still show as disabled
.
Is there any simple solution for that?
回答1:
HTML :-
@Html.EditorFor(m => m.Description,
new { htmlAttributes =
new
{
@class = "form-control"
}
});
Try using jQuery as shown :-
if(@Json.Encode(Model.IsReadOnly))
{
$('#Description').attr('readonly','readonly')
}
if(@Json.Encode(Model.IsDisabled))
{
$('#Description').attr('disabled','disabled')
}
来源:https://stackoverflow.com/questions/29247726/how-do-i-conditionally-specify-whether-html-input-is-disabled-readonly-or-not