1
I have a read-only view in some cases. I don’t want to use javascript as it can be disabled on the user side. At last...
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>
But when I try to pass HTML attributes by variable, as follows, it doesn’t work:
var attribs = new Dictionary<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>
Notice the only thing I change is:
@class = "form-control input-Sm", @disabled ="disabled"
for
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 not receiving @attribs.
Thanks! It really worked. But could you tell me why in Editorfor I had no problem? Afinal qual a maneira correta... @Html.EditorFor(model => model.DataCadastro, new { htmlAttributes = @attribs }) ou @Html.EditorFor(model => model.DataCadastro, htmlAttributes: @attribs)?
– AlamBique
The correct way is
htmlAttributes: @attribs
, can’t tell you why it worked the other way in Editorfor but declaring a new object with a property htmlAttributes (new { htmlAttributes = ... }
) is not a valid overload. Right is an object with attributes in the key/value type or a <string,>– Good Bye Blue sky