Error trying to pass an ordered collection to a View

3

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>

Displayed Error

    
asked by anonymous 07.10.2015 / 03:36

2 answers

-1

I discovered the error in my code. I was using hidden field in a Data Insertion View that there is no need. By realizing the error and deleting, I was able to pass a collection directly through my Add controller method

@Html.HiddenFor(model => model.EmpregadoID)
    
10.10.2015 / 04:46
2

These two forms as examples below are correct:

@Html.DropDownList("TituloDeCortesia", String.Empty)
@Html.DropDownList("TituloDeCortesia", (SelectList)ViewBag.TituloDeCortesia)

That is, your code is correct for what is in the question, what should be happening is another type of error of not passing the information correctly through the ViewBag.

Another way

public ActionResult Adicionar()
{

    IList<SelectListItem> Lista = new List<SelectListItem>();

    Lista.Add(new SelectListItem() { Text = "Ms.", Value = "Ms." });
    Lista.Add(new SelectListItem() { Text = "Dr.", Value = "Dr." });
    Lista.Add(new SelectListItem() { Text = "Mrs.", Value = "Mrs." });
    Lista.Add(new SelectListItem() { Text = "Mr.", Value = "Mr." });

    ViewBag.ListaTitulo = Lista;

    return View();
}

View: (Only that section)

<div class="editor-label">
    @Html.Label("Titulo de Cortesia")
</div>
<div class="editor-field">
    @Html.DropDownListFor(m => Model.TituloDeCortesia, (IEnumerable<SelectListItem>)ViewBag.ListaTitulo)
    @Html.ValidationMessageFor(model => model.TituloDeCortesia)
</div>

Note that the Label I had to put directly in this case, please test !!!

    
07.10.2015 / 21:00