Send list of objects to View, by ViewBag Asp.net
I'd like to get some data: Class:
public class Produto
{
public Produto()
{
this.Categoria = new HashSet<Categoria>().ToList();
}
#region Atributos
[Key]
public int ProdutoId { get; set; }
[Required(ErrorMessage ="o nome deve ser preenchido")]
public string NomeDoProduto { get; set; }
[Required(ErrorMessage = "o codigo deve ser preenchido")]
public string CodProduto { get; set; }
[Required(ErrorMessage ="o preço deve ser preenchido")]
public decimal PrecoDeAtacado { get; set; }
[Required(ErrorMessage = "o preço deve ser preenchido")]
public decimal PrecoDeVarejo { get; set; }
[MaxLength(1200)]
public string Informacoes { get; set; }
[MaxLength(1200)]
public string Decricao { get; set; }
public bool? Disponibilidade { get; set; }
public int Quatidade { get; set; }
#endregion
#region Chaves Estrangeiras
public int CorId { get; set; }
public virtual Cor Cor { get; set; }
public int TamanhoId { get; set; }
public virtual Tamanho Tamanho { get; set; }
public int ImagemId { get; set; }
public virtual Imagem Imagem { get; set; }
public virtual IEnumerable<Comentario> Comentario { get; set; }
public virtual List<Categoria> Categoria { get; set; }
#endregion
}
The product has color, and size.
This product has the cód 123
I have more products with cod 123
For example:
Produto: Sapato Mocacin - Id: 1 - Cod: 123 - Tamanho: 39 - Cor: Preto
Produto: Sapato Mocacin - Id: 2 - Cod: 123 - Tamanho: 40 - Cor: Preto
Produto: Sapato Mocacin - Id: 3 - Cod: 123 - Tamanho: 39- Cor: Marrom
In my index, I am showing only 1 product of type
Sapato Mocacin que tem o cod= 123
In my details View I show the details of the product, from which the user clicked,
I would like to know how I do to show the sizes and colors, of the product that has the cod 123
I want to make a query, play in a viewbag, and in view detalhesm create a dropDownList for the options of this view bag, either the color or the size.
Controller:
public ActionResult Detalhes(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Produto produto = db.ProdutoDb.Find(id);
if (produto == null)
{
return HttpNotFound();
}
var geral = db.ProdutoDb.Where(x => x.CodProduto == produto.CodProduto);
return View(produto);
}
2 answers
Query your database list:
public List<Produto> ListaProduto()
{
var listaProduto = new List<Produto>();
var objLista = context.TB_Produto.ToList();
foreach (var item in objLista)
{
var produto = new Produto();
produto.ProdutoId = item.ProdutoId;
produto.NomeDoProduto = item.CodProduto + "-" + item.Tamanho + "-" + item.Cor;
listaProduto.Add(produto);
}
Return listaProduto
}
In controller
you can do like this:
var listaProdutos = new ListaDeProdutos();
ViewBag.ddlProdutos = new SelectList(listaProdutos, "ProdutoId", "NomeDoProduto");
In view
you can do like this:
<div class="col-md-5">
<div class="editor-label">
<label for="ddlProdutos ">Selecione o Produto</label>
</div>
<div class="editor-field">
@Html.DropDownList("ddlProdutos", string.Empty)
</div>
</div>
Hope it helps you!
You can select colors and sizes from your var geral
. Like this:
var geral = db.ProdutoDb.Where(x => x.CodProduto == produto.CodProduto);
ViewBag.TodasCores =
geral.Where(x => x.CodProduto == produto.CodProduto)
.Select(s => s.Cor).Select(s => new SelectListItem
{
Value = s.CorId.ToString(),
Text = s.Descricao
});
ViewBag.TodosTamanhos =
geral.Where(x => x.CodProduto == produto.CodProduto)
.Select(s => s.Tamanho).Select(s => new SelectListItem
{
Value = s.Id.ToString(),
Text = s.Descricao.ToString()
});
In your View do this to create the DropDowns with the options:
@Html.DropDownList("Cores", ViewBag.TodasCores as SelectList, new { @class = "form-control" })
@Html.DropDownList("Tamanhos", ViewBag.TodosTamanhos as SelectList, new { @class = "form-control" })
I don't usually leave my lists of DropDowns
in ViewBags
, I usually have all my fields in my ViewModels
, but in your scenario and with ViewBags
, as you asked, this would be one of the ways to do.