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>