I'm trying to pass% s to an ordered collection, but I'm always having the same error:
There is no ViewData item of type 'IEnumerable' that has the 'TitleCortesia' key
The only way I can successfully run my code is by using the following line:
@Html.DropDownListFor(model => model.TituloDeCortesia, new SelectList(new[] { "Dr.", "Mr.", "Ms.", "Mrs." }), String.Empty)
But without ordering. In this example I ordered manually, but in case of large and dynamic listing, this solution I found would not solve.
Here is my code below:
Controller
public ActionResult Adicionar()
{
List< string > ListaTitulo = new List< string > { "Ms.", "Dr.", "Mrs.", "Mr." };
ListaTitulo.Sort();
ViewBag.TituloDeCortesia = new SelectList(ListaTitulo);
return View();
}
View
@model MvcModeloEmpresa.Dominio.Empregado
@{
ViewBag.Title = "Adicionar";
}
<h2>Adicionar</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Empregado</legend>
@Html.HiddenFor(model => model.EmpregadoID)
<div class="col-md-10">
@Html.LabelFor(model => model.PrimeiroNome)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PrimeiroNome)
@Html.ValidationMessageFor(model => model.PrimeiroNome)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UltimoNome)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UltimoNome)
@Html.ValidationMessageFor(model => model.UltimoNome)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Titulo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Titulo)
@Html.ValidationMessageFor(model => model.Titulo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TituloDeCortesia)
</div>
<div class="editor-field">
@Html.DropDownList("TituloDeCortesia", String.Empty)
@Html.ValidationMessageFor(model => model.TituloDeCortesia)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DataNascimento)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DataNascimento)
@Html.ValidationMessageFor(model => model.DataNascimento)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DataContratacao)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DataContratacao)
@Html.ValidationMessageFor(model => model.DataContratacao)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Endereco)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Endereco)
@Html.ValidationMessageFor(model => model.Endereco)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Cidade)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Cidade)
@Html.ValidationMessageFor(model => model.Cidade)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Regiao)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Regiao)
@Html.ValidationMessageFor(model => model.Regiao)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CodigoPostal)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodigoPostal)
@Html.ValidationMessageFor(model => model.CodigoPostal)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Pais)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Pais)
@Html.ValidationMessageFor(model => model.Pais)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TelefoneResidencial)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TelefoneResidencial)
@Html.ValidationMessageFor(model => model.TelefoneResidencial)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Extensao)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Extensao)
@Html.ValidationMessageFor(model => model.Extensao)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Notas)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Notas)
@Html.ValidationMessageFor(model => model.Notas)
</div>
<p>
<input type="submit" value="Salvar" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Voltar", "Index")
</div>