2
I’m a beginner in ASP.NET MVC development and I need some help.
I’m having trouble creating a Foreach
.
Follow my code below.
@foreach (var item in Model.Fornecedores)
{
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem.Codigo)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem.NomeFantasia)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem.RazaoSocial)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem.CNPJ)
</td>
</tr>
}
My model that’s the one:
public partial class Fornecedor
{
public Fornecedor()
{
this.Entrada = new HashSet<Entrada>();
this.Produto = new HashSet<Produto>();
}
public int Codigo { get; set; }
[Required(ErrorMessage="Nome fantasia é obrigatório", AllowEmptyStrings=false)]
public string NomeFantasia { get; set; }
[Required(ErrorMessage = "Razão Social é obrigatório", AllowEmptyStrings = false)]
public string RazaoSocial { get; set; }
[Required(ErrorMessage = "Inscrição Estadual é obrigatório", AllowEmptyStrings = false)]
public string IE { get; set; }
[Required(ErrorMessage = "CNPJ é obrigatório", AllowEmptyStrings = false)]
public string CNPJ { get; set; }
public Nullable<bool> Ativo { get; set; }
public virtual ICollection<Entrada> Entrada { get; set; }
public virtual ICollection<Produto> Produto { get; set; }
public virtual ICollection<Fornecedor> Fornecedores { get; set; }
}
Man Controller:
public ActionResult Index()
{
return View();
}
The error is as follows: Undefined object reference for an object instance
What are we missing? I need help.
Thanks a lot, you’ve already helped me a lot. Apparently you’re almost right. Now I am trying to solve an error that is giving when Debug passes on the line: 'var suppliers = db.Fornecedores.Tolist();' The error that is giving is this one: An Exception of type "System.Data.Entity.ModelConfiguration.Modelvalidationexception" occurred in Entityframework.dll but was not handled in user code Additional information: One or more validation errors Were Detected During model Generation:
– user31040
Quite possibly it is because you do not have a primary key defined in the entity
fornecedor
. In the model where you have the fieldcodigo
, putspublic int FornecedorID { get; set; }
– CesarMiguel
I put a Dataannotations [Key] for all Primary Keys and no longer gave the error, but did not select from any vendor that is in the bank. I’m looking for the why, if I can help.
– user31040
Yes, it works too. By debugging what gives you the list on
fornecedores
in the controller?– CesarMiguel
The list returns NULL and says that "Enumeration did not generate results" ?
– user31040
the variable is returning all the list you have in the entity
Fornecedor
, Are you sure you have data in the comic book?– CesarMiguel
There is data in the database, but returns nothing and also no error. I am not using Where then there are no filters.
– user31040
Yes, the start has to be like this because there are no filters...
– CesarMiguel
The error may be in the SELECT that Linq-to-sql is doing.
SELECT 
 [Extent1].[Codigo] AS [Codigo], 
 [Extent1].[NomeFantasia] AS [NomeFantasia], 
 [Extent1].[RazaoSocial] AS [RazaoSocial], 
 [Extent1].[IE] AS [IE], 
 [Extent1].[CNPJ] AS [CNPJ], 
 [Extent1].[Ativo] AS [Ativo], 
 [Extent1].[Fornecedor_Codigo] AS [Fornecedor_Codigo]
 FROM [dbo].[Fornecedors] AS [Extent1]}
Note my last line of code My table calls Vendor and not Vendor. It must be doing the pluralization via Entity Framework– user31040
I advise you to make a new post with that same question.. I don’t understand what the problem is
– CesarMiguel