I have an entity called Request. I created in the Model folder a class that loads the Orders, called GetPedidos . Well, when I created the controller, I added a view to the Action Index . When building the view, I had her use the GetPedidos model. When I squeeze, it gives me this error:
The template item entered in the dictionary is from type 'System.Collections.Generic.List'1 [TSL.Domain.Entities.Prev]', but this dictionary requires an item of type 'System.Collections.Generic.IEnumerable'1 [TSL.Models.GetData]'.
Exception Details: System.InvalidOperationException: The model inserted in the dictionary is type 'System.Collections.Generic.List'1 [TSL.Domain.Entities.Prev]', but this dictionary requires an item of type 'System.Collections.Generic.IEnumerable'1 [TSL.Models.GetData]'.
I understand the error, I just do not know how to change it, that is, whether or not to use the GetPedidos
EDIT1
GetPedidas
public class GetPedidos : RepositoryData<Pedido, int>
{
public override void Delete(Pedido entity)
{
using (var conn = new SqlConnection(StringConnection))
{
string sql = "DELETE Pedido Where Id_Pedido=@Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Id", entity.Id);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
}
public override void DeleteById(int id)
{
using (var conn = new SqlConnection(StringConnection))
{
string sql = "DELETE Pedido Where Id=@Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Id", id);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
}
public override List<Pedido> GetAll()
{
string sql = "Select Id_Pedido, Id_Cliente, Num_Pedido, Dt_Entrega, Valor_Total FROM Pedido ORDER BY Num_Pedido";
using (var conn = new SqlConnection(StringConnection))
{
var cmd = new SqlCommand(sql, conn);
List<Pedido> list = new List<Pedido>();
Pedido p = null;
try
{
conn.Open();
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
p = new Pedido();
p.Id = (int)reader["Id-Pedido"];
p.Numero = reader["Num_Pedido"].ToString();
p.DtEntrega = Convert.ToDateTime(reader["Dt_Entrega"]);
p.ValorTotal = Convert.ToDecimal(reader["ValorTotal"]);
p.IdCliente = (int)reader["Id_Cliente"];
list.Add(p);
}
}
}
catch (Exception e)
{
throw e;
}
return list;
}
}
public override Pedido GetById(int id)
{
using (var conn = new SqlConnection(StringConnection))
{
string sql = "Select Id_Pedido, Id_Cliente, Num_Pedido, Dt_Entrega, Valor_Total FROM Pedido WHERE Id_Pedido=@Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Id", id);
Pedido p = null;
try
{
conn.Open();
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (reader.HasRows)
{
if (reader.Read())
{
p = new Pedido();
p.Id = (int)reader["Id-Pedido"];
p.Numero = reader["Num_Pedido"].ToString();
p.DtEntrega = Convert.ToDateTime(reader["Dt_Entrega"]);
p.ValorTotal = Convert.ToDecimal(reader["ValorTotal"]);
p.IdCliente = (int)reader["Id_Cliente"];
}
}
}
}
catch (Exception e)
{
throw e;
}
return p;
}
}
public override void Save(Pedido entity)
{
using (var conn = new SqlConnection(StringConnection))
{
string sql = "INSERT INTO Pedido (Id_Cliente, Num_Pedido, Dt_Entrega, Valor_Total) VALUES (@Cliente, @NumPedido, @DtEntrega, @Valor)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Cliente", entity.IdCliente);
cmd.Parameters.AddWithValue("@NumPedido", entity.Numero);
cmd.Parameters.AddWithValue("@DtEntrega", entity.DtEntrega);
cmd.Parameters.AddWithValue("@Valor", entity.ValorTotal);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
}
public override void Update(Pedido entity)
{
using (var conn = new SqlConnection(StringConnection))
{
string sql = "UPDATE Pedido SET Id_Cliente=@Cliente, Num_Pedido=@Numero, Dt_Entrega=@DtEntrega, Valor_Total=@Valor Where Id_Pedido=@Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Id", entity.Id);
cmd.Parameters.AddWithValue("@Cliente", entity.IdCliente);
cmd.Parameters.AddWithValue("@Numero", entity.Numero);
cmd.Parameters.AddWithValue("@DtEntrega", entity.DtEntrega);
cmd.Parameters.AddWithValue("@Valor", entity.ValorTotal);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
}
}
Controller
public class PedidoController : Controller
{
// GET: Pedido
private GetPedidos respository = new GetPedidos();
// GET: Pessoa
public ActionResult Index()
{
return View(respository.GetAll());
}
// GET: Pessoa/Create
public ActionResult Create()
{
return View();
}
// POST: Pessoa/Create
[HttpPost]
public ActionResult Create(Pedido pedido)
{
if (ModelState.IsValid)
{
respository.Save(pedido);
return RedirectToAction("Index");
}
else
{
return View(pedido);
}
}
// GET: Pessoa/Edit/5
public ActionResult Edit(int id)
{
var pedido = respository.GetById(id);
if (pedido == null)
{
return HttpNotFound();
}
return View(pedido);
}
// POST: Pessoa/Edit/5
[HttpPost]
public ActionResult Edit(Pedido pedido)
{
if (ModelState.IsValid)
{
respository.Update(pedido);
return RedirectToAction("Index");
}
else
{
return View(pedido);
}
}
// POST: Pessoa/Delete/5
[HttpPost]
public ActionResult Delete(int id)
{
respository.DeleteById(id);
return Json(respository.GetAll());
}
}
Repository
public abstract class RepositoryData<TEntity, TKey>
where TEntity : class
{
protected string StringConnection { get; } = ConfigurationManager.ConnectionStrings["ConnTsL"].ConnectionString;
public abstract List<TEntity> GetAll();
public abstract TEntity GetById(TKey id);
public abstract void Save(TEntity entity);
public abstract void Update(TEntity entity);
public abstract void Delete(TEntity entity);
public abstract void DeleteById(TKey id);
}
A View
@model IEnumerable<TSL.Models.GetPedidos>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
EDIT1
The Error
The template item entered in the dictionary is from type 'System.Collections.Generic.List
1[TSL.Domain.Entities.Pedido]', mas esse dicionário requer um item do tipo 'System.Collections.Generic.List
1 [TSL.Models.GetData]'.
The view is like this
@model List<TSL.Models.GetPedidos>