1
Class
public class ConfiguracaoEstado
{
....
[Required(ErrorMessage = "Selecione ao menos um estado.")]
[Display(Name = "Estado")]
public int EstadoID { get; set; }
...
}
Controller
private void createViewBag(ConfiguracaoEstado configuracaoestado)
{
ViewBag.EstadoID = new SelectList(db.Estado, "Id", "uf", configuracaoestado.EstadoID);
....
}
View
<div class="form-group">
@Html.LabelFor(model => model.EstadoID, "Estado", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("EstadoID", String.Empty);
@Html.ValidationMessageFor(model => model.EstadoID)
</div>
</div>
I wish that when executing ActionResult Edit(int? id)
the DropDownList
was disabled.
I tried so:
@Html.DropDownList("EstadoID", String.Empty, new { disabled = Model != null});
and it didn’t work.
So I tried it like this:
@Html.DropDownListFor(model => model.EstadoID,
(SelectList)ViewBag.EstadoID,
String.Empty, new {disabled = Model != null}
)
Then the next thing happens:
- Disables control, but does not display the content of
DropDownList
. - Retires the
String.Empty
, displays the contents, but not what is saved, but the first of the table.
With bringing the correct content of DropDownList
and disable it?
In the statement of ddlProps is returning the following error: Cannot Convert source type '{redeonly:string, disabled:string}' to target type 'Anonymous type'
– Jothaz
Try declaring ddlProps as Object because this error is by trying to convert an anonymous type or other.
– Tobias Mesquita
Now the error is in the reference to
ddlProps
in the creation ofDropDownList
com pode ser visot no imagem: https://onedrive.live.com/redir?resid=6B6FE80FB785B22C! 89450&authkey=! Ap9ieiwc_mnntly&v=3&ithint=photo%2cpng– Jothaz
I managed to solve the problem and already edited the answer with the correct code.
– Jothaz