There is no ViewData item of type 'IEnumerableSelectListItem' that has the key

1

Good afternoon friends.

I'm doing a Demo of a solution in Visual Studio with C #.

The project manages an architecture pattern of 5 "layers (I do not know if that's the technical name)" , which are:

  • Entities,
  • the data (Data Access and DAO),
  • Business, you are asked to do a service in WCF, and the project web that is the consumes the service (Controller and Views) by the moment is like this.

I'm trying to do DropDownListFor , but I get the error

  

There is no ViewData item of type 'IEnumerable' that   has the key

I have already tried to try solutions that appear in various forums and have not served me, which is why I come to you.

I enclose the link of the code of each one of the "layers".

Entities ("Main" Vehicle)

namespace Entidades.Entidades
{
    using System;
    using System.Collections.Generic;

    public partial class Vehiculo
    {  
        public int id_Vechiculo { get; set; }
        public string v_CodigoVehiculo { get; set; }
        public string v_NumeroPlaca { get; set; }
        public int i_Kilometraje { get; set; }
        public string v_EstadoVehiculo { get; set; }

        public Nullable<int> id_TipoPlaca { get; set; }
        public Nullable<int> id_Marca { get; set; }
        public Nullable<int> id_Tanque { get; set; }
        public Nullable<int> id_Categoria { get; set; }

        public virtual Categoria Categoria { get; set; }
        public virtual Marca Marca { get; set; }
        public virtual Tanque Tanque { get; set; }
        public virtual TipoPlaca TipoPlaca { get; set; }
    }
}

Entities (Plate Type)

namespace Entidades.Entidades
{
    using System;
    using System.Collections.Generic;

    public partial class TipoPlaca
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public TipoPlaca()
        {
            this.Vehiculo = new HashSet<Vehiculo>();
        }

        public int id_TipoPlaca { get; set; }
        public string v_NombrePlaca { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Vehiculo> Vehiculo { get; set; }
    }
}

DAO

public List<Entidades.Entidades.TipoPlaca> ListTipoPlaca()
{
    try
    {
        List<Entidades.Entidades.TipoPlaca> listarTiposPlaca;
        using (ModeloDatos.VehiculosDBEntities db = new ModeloDatos.VehiculosDBEntities())
        {
            listarTiposPlaca = (from tp in db.TipoPlaca
                                select new Entidades.Entidades.TipoPlaca
                                {
                                    id_TipoPlaca = tp.id_TipoPlaca,
                                    v_NombrePlaca = tp.v_NombrePlaca
                                }).ToList();
        }
        return listarTiposPlaca;
    }
    catch (Exception ex)
    {

        throw;
    }
}

Business

public List<Entidades.Entidades.TipoPlaca> ListConsultarTipoPlaca()
{
    return vehiculosDAO.ListTipoPlaca();
}

Service

   [OperationContract]
   List<Entidades.Entidades.TipoPlaca> ListarConsultarTipoPlaca();

   public List<Entidades.Entidades.TipoPlaca> ListarConsultarTipoPlaca()
   {
       return servicioVehiculo.ListConsultarTipoPlaca();
   }

WEB

public List<SelectListItem> ListaTipoPlaca()
{
    List<SelectListItem> _ListTipoPlaca = new List<SelectListItem>();
    try
    {
        var getListaTipoPlaca = servicioVehiculo.ListarConsultarTipoPlaca();
        foreach (var item in getListaTipoPlaca)
        {
            _ListTipoPlaca.Add(new SelectListItem() { Value = item.id_TipoPlaca.ToString(), Text = item.v_NombrePlaca });
        }

    }
    catch (Exception)
    {

        throw;
    }
    return _ListTipoPlaca;
}

[HttpPost]
public ActionResult Create(Vehiculo oVehiculo)
{
    try
    {
        ViewBag.TipoPlacas = ListaTipoPlaca();

        servicioVehiculo.Crear(oVehiculo);
        TempData["Message"] = "Vehiculo Creado";
    }
    catch
    {
        return View();
    }
    return RedirectToAction("Index");
}

<div class="col-md-5">
@Html.DropDownListFor(model => model.TipoPlaca.id_TipoPlaca, (IEnumerable<SelectListItem>)ViewBag.TipoPlacas, new {@class = "form-control", @title=""})
</div>
    
asked by Ingrid Viviana Rozo Moreno 05.02.2016 в 16:48
source

3 answers

1

Good afternoon Friends. What happened is that I put the method ViewBag.TipoPlacas = ListTipoPlaca (); with an HttpPost (That method will only be executed when you click on submit or an event that leads to the server), if you are waiting for it to show something it will never do it. To consult never use the HttpPost To send data to the server use the HttpPost CP. Javier Florian Franco, 2016, Developer .Net Thanks for all your contributions.

    
answered by 05.02.2016 в 18:23
1

> > > > which are: The entities, the data (Data Access and DAO), the Business, you are asked to do a service in WCF

Entities are not a layer, they are counted as part of the business or persistence.

> > There is no ViewData item of type 'IEnumerable' that has the key

What I would recommend is that you eliminate the RedirectToAction that you make in the action, simply use return View("Index");

public ActionResult Create()
{
    ViewBag.TipoPlacas = ListaTipoPlaca();
    TempData["Message"] = "";

    return View("Index");
}

[HttpPost]
public ActionResult Create(Vehiculo oVehiculo)
{
    ViewBag.TipoPlacas = ListaTipoPlaca();

    servicioVehiculo.Crear(oVehiculo);
    TempData["Message"] = "Vehiculo Creado";

    return View("Index");
}
    
answered by 05.02.2016 в 17:32
0

You can try this way:

 @Html.DropDownListFor(model => model.TipoPlaca.id_TipoPlaca, ViewBag.TipoPlacas as IEnumerable<SelectListItem>

In general, it gives me razor problems when wanting to use a property of an object. as you do with the plate model.TypePlaca.id_TypePlaca, and so I usually put the model a property Id_TipoPlaca.

    
answered by 05.02.2016 в 17:07