-2
Controller:
[HttpPost]
public ActionResult Create(Pedido pedido)
{
List<Produto> lista = new List<Produto>();
if (ModelState.IsValid)
{
Cliente cliente = unitOfWork.ClienteRepository.Busca(pedido.ClienteId);
Produto produto = unitOfWork.ProdutoRepository.Busca(pedido.ProdutoId);
lista.Add(produto);
pedido.produtos = lista;
pedido.cliente = cliente;
unitOfWork.PedidoRepository.Adiciona(pedido);
unitOfWork.Salva();
return RedirectToAction("Index");
}
ViewBag.ProdutoId = new SelectList(context.Produtos, "ID", "Nome", pedido.ProdutoId);
ViewBag.ClienteId = new SelectList(context.Clientes, "ID", "NomeCliente", pedido.ClienteId);
return View(pedido);
}
By then OK...I can save my order with an instantiated customer and a list of products...but when this controller passes this order to my View INDEX...the customer object and the list of products arrives empty.
VIEW
@model IEnumerable<ProjetoZeus.Models.Pedido>
@{
ViewBag.Title = "Index";
}
<h2>Lista de Pedidos</h2>
<p>
@Html.ActionLink("Adicionar Novo", "Create")
</p>
<fieldset>
<legend>Pedidos</legend>
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.ID)</th>
<th>Produto</th>
<th>Preço</th>
<th>@Html.DisplayNameFor(model => model.cliente)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(model => item.ID)
</td>
<td>
@Html.DisplayFor(model => item.produtos[0].Nome)
</td>
<td>
@Html.DisplayFor(model => item.produtos[0].Preco)
</td>
<td>
@Html.DisplayFor(model => item.cliente.NomeCliente)
</td>
</tr>
}
</table>
</fieldset>
Action of the Index
public ActionResult Index()
{
return View(unitOfWork.PedidoRepository.Pedidos);
}
unitofwork class
public PedidoRepository PedidoRepository
{
get
{
if (pedidoRepository == null)
{
pedidoRepository = new PedidoRepository(context);
}
return pedidoRepository;
}
}
Pedidorepository.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjetoZeus.Models
{
public class PedidoRepository
{
private bool disposed = false;
private Contexto context;
public PedidoRepository(Contexto context)
{
this.context = context;
}
public void Salva()
{
context.SaveChanges();
}
public void Adiciona(Pedido pedido)
{
context.Pedidos.Add(pedido);
}
public Pedido Busca(int id)
{
return context.Pedidos.Find(id);
}
public void Remove(int id)
{
Pedido pedido = Busca(id);
context.Pedidos.Remove(pedido);
}
public List<Pedido> Pedidos
{
get
{
return context.Pedidos.ToList();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
}
}
Post your "Index" action code. Your "Index" action has to fetch your requests to be viewed in the view.
– Hendrik Araujo
posted...the only things that are lost when you arrive in the view is the Customer object and the Product Type List
– Daniel
Customer and Products are loaded in your order query ? If you are using Entity, you have Lazy load enabled ?
– Hendrik Araujo
Yes they are loaded and I am using Entity...what is Lazy load ?
– Daniel
http://answall.com/questions/20646/como-funciona-o-lazy-load-entity-framework
– Hendrik Araujo
If it arrives empty, the repository is not doing what it should. How is the repository code?
– Leonel Sanches da Silva
In the Controller Create action I changed to : Return View("Index",unitOfWork.PedidoRepository.Orders); instead of Return Redirecttoaction("Index"); and it worked...but I did not understand why...the new requests I add it brings now the old ones remain null
– Daniel
looking well only the last request I add brings the right data
– Daniel
There’s something very wrong with the repository, and this
return View()is terrible. You are using what for the bank layer?– Leonel Sanches da Silva
I’m using that Entity Dbcontext
– Daniel