Why this @Html.DropDownListFor does not accept htmlAttributes?

1

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.

    
asked by anonymous 16.11.2017 / 11:50

1 answer

2

Try this:

@Html.DropDownListFor(model => model.IdStatusRegistro, new SelectList(ViewBag.ListaSelectList, "Id", "Descricao"), htmlAttributes: attribs)

Apparently it's just an error in passing the parameter.

    
16.11.2017 / 11:54