The ViewData item that has the key 'officeId' is of type 'System.String' but must be of type 'IEnumerableSelectListItem'

-2
  

Controller:

 public ActionResult Cadastrar()
    {

        ViewBag.officelist = new SelectList(new OfficeREP().ListarTodos(),
           "id",
           "estado"
       );

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Cadastrar(OpportunityMOD opportunidade)
    {

        if (ModelState.IsValid)
        {
            var opportunity01 = new OpportunityREP();
            opportunity01.Salvar(opportunidade);
            TempData["mensagem"] = "Cadastro realizado com Sucesso!";
            return RedirectToAction("Index");
        }

        return View(opportunidade);

    }
  

Model

    [DisplayName("Escritório")]
    public string officeId { get; set; }
  

View

                <div class="col-md-2">
                @Html.LabelFor(model => model.officeId)
                @Html.DropDownListFor(model => model.officeId,(SelectList)ViewBag.officeList, string.Empty, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.officeId, "", new { @class = "text-danger" })
    
asked by anonymous 03.02.2016 / 18:10

1 answer

1

Hello,

The problem is that you are trying to list a string.

In this line.

@Html.DropDownListFor(model => model.officeId,(SelectList)ViewBag.officeList, string.Empty, new { @class = "form-control" }) 

Then, do so

Model

[DisplayName("Escritório")]
    public IEnumerable<SelectListItem> officeId { get; set; }

Controller

ViewBag.officelist = new SelectList(new OfficeREP().ListarTodos(), 
"id", 
"estado" 
);

View

@Html.DropDownListFor(model => model.officeId,(SelectList)ViewBag.officeList, string.Empty, new { @class = "form-control" })
    
03.02.2016 / 18:57