I have a View that will be read-only in some cases. And I do not want to use javascript because it can be disabled on the user side. In the end ...
When I do this (for testing) it works perfectly.
<div class="form-group">
@Html.LabelFor(model => model.IdStatusRegistro, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="input-group col-md-10">
@Html.DropDownListFor(model => model.IdStatusRegistro, new SelectList(ViewBag.ListaSelectList, "Id", "Descricao"), new { @class = "form-control input-sm", @disabled ="disabled" })
@Html.ValidationMessageFor(model => model.IdStatusRegistro, "", new { @class = "text-danger" })
</div>
</div>
ButwhenItrytopasstheHTMLattributesbyvariable,asfollows,itdoesnotwork:
varattribs=newDictionary<string,object>();attribs.Add("class", "form-control input-sm");
if (ViewBag.ReadOnly)
{
attribs.Add("disabled", "disabled");
}
<div class="form-group">
@Html.LabelFor(model => model.IdStatusRegistro, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="input-group col-md-10">
@Html.DropDownListFor(model => model.IdStatusRegistro, new SelectList(ViewBag.ListaSelectList, "Id", "Descricao"), new { htmlAttributes = @attribs })
@Html.ValidationMessageFor(model => model.IdStatusRegistro, "", new { @class = "text-danger" })
</div>
</div>
NoticethattheonlythingIchangeis:
@class="form-control input-sm", @disabled="disabled"
by
htmlAttributes = @attribs
And what makes me curious is that it always worked for me. Even in the same View works perfectly for @ Html.EditorFor. Only this DropDownListFor that is not getting @attribs.