Reputation: 412
Since English is not my native language, I'm very sorry for any mistake I will exihbit in the next senteces.
I created a database with products (produtos) and types of Products (Categoria)
My objective was to create a relationship (codefirst) in MVC from One to Many. Where a type of product (Categoria) can have multiple Products (Produtos)
So I had created this:
namespace HardwareWarehouse.Models
{
public class Produto
{
public Produto()
{
}
[Key]
public int IDProduto { get; set; }
[Required]
[StringLength(50)]
public string Nome { get; set; }
[Required]
public double Preco { get; set; }
[Required]
public double Peso { get; set; }
[Required]
[StringLength(255)]
public string Descricao { get; set; }
public int Imagem { get; set; }
public DateTime UltimaAtualizacao { get; set; }
public int Stock { get; set; }
public int CategoriaID { get; set; }
public virtual Produto Produtos { get; set; }
}
}
And for the Category/type of Product (Categoria), I created this..
namespace HardwareWarehouse.Models
{
public class Categoria
{
public Categoria()
{
Produtos = new List<Produto>();
}
[Key]
public int CategoriaID { get; set; }
[Required]
[StringLength(50)]
public string Nome { get; set; }
public virtual ICollection<Produto> Produtos { get; set; }
}
}
So I think my relationship between both tables are fine so far.
Next, I had created a controller, and used a default view to store some data. Which looks like this:
And this is my default view that shows that info..
@model IEnumerable<HardwareWarehouse.Models.Produto>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Nome)
</th>
<th>
@Html.DisplayNameFor(model => model.Preco)
</th>
<th>
@Html.DisplayNameFor(model => model.Peso)
</th>
<th>
@Html.DisplayNameFor(model => model.Descricao)
</th>
<th>
@Html.DisplayNameFor(model => model.Imagem)
</th>
<th>
@Html.DisplayNameFor(model => model.UltimaAtualizacao)
</th>
<th>
@Html.DisplayNameFor(model => model.Stock)
</th>
<th>
@Html.DisplayNameFor(model => model.CategoriaID)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.Preco)
</td>
<td>
@Html.DisplayFor(modelItem => item.Peso)
</td>
<td>
@Html.DisplayFor(modelItem => item.Descricao)
</td>
<td>
@Html.DisplayFor(modelItem => item.Imagem)
</td>
<td>
@Html.DisplayFor(modelItem => item.UltimaAtualizacao)
</td>
<td>
@Html.DisplayFor(modelItem => item.Stock)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoriaID)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.IDProduto }) |
@Html.ActionLink("Details", "Details", new { id=item.IDProduto }) |
@Html.ActionLink("Delete", "Delete", new { id=item.IDProduto })
</td>
</tr>
}
</table>
</body>
</html>
The Millions dollar question is? How do I make the view to show the Categoria.Nome instead of the CategoriaID number on that view?
Thanks in advance..
Upvotes: 0
Views: 1351
Reputation: 150
Adding to Alisson answer, ViewModels are used to send multiple models to the view Here is the Link ViewModel in ASP.NET MVC
Upvotes: 1