3
I’m trying to get to the View
an ordered collection, but I’m always having the same error:
There is no Viewdata item of type 'Ienumerable' that has the key 'Titulodecortesia'
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)
However, no sorting. In this example I sorted manually, but in the case of larger and dynamic listings, this solution I found would not solve.
Follow 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>
I tested your code here and it worked perfectly. You are entering the action
Adicionar
when threshing?– Filipe Oliveira
When I select an item and submit the data, I have the error of the image I entered at the end of my question.
– Kelly Soares
Ask your question any and all information from controller traffic to view (Full code)
– Cezar
Cezar, do you want me to put all the code in my View? How do I see the traffic from my controller method?
– Kelly Soares
As @Cezar said, your code is correct. Look at an example works here (https://dotnetfiddle.net/8wwRPR). What version of Asp.Net MVC are you using?
– Randrade