1
I have two related methods. Patrimonio
and Categoria
:
[Table("Patrimonios")]
public class Patrimonio
{
[Key]
public Guid PatrimonioId { get; set; }
public Guid CategoriaId { get; set; }
[Required]
[StringLength(500)]
[Index(IsUnique =true)]
public String Nome { get; set; }
[Required]
[DataType(DataType.Currency)]
public decimal ValorTotalDaNota { get; set; }
public virtual Categoria Categorias { get; set; }
}
[Table("Categorias")]
public class Categoria
{
[Key]
public Guid CategoriaId { get; set; }
//public Guid PatrimonioId { get; set; }
[Required]
[StringLength(500)]
[Index(IsUnique = true)]
public String Nome { get; set; }
public ICollection<Patrimonio> Patrimonios { get; set; }
}
That relate. But when generating the View
Create
@model ControlePatrimonial.Models.Patrimonio @{ ViewBag.Title = "Create"; }
<h2>Create</h2>
@using (Html.BeginForm()) { @Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Produto</h4>
<hr />@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CategoriaId, "CategoriaId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoriaId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.CategoriaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Preco, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Preco, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Preco, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
It returns me the following error when I try to access it:
Server Error in Application '/'. There is no 'Ienumerable' Viewdata item that has the 'Categoriaid' key. Description: An untreated exception occurred during the execution of the current web request. Examine stack tracking for more information about the error and where it originated in the code. Exception Details: System.Invalidoperationexception: There is no 'Ienumerable' Viewdata item that has the 'Categoriaid' key'.
Linha 20: @Html.LabelFor(model => model.CategoriaId, "CategoriaId", htmlAttributes: new { @class = "control-label col-md-2" })
Linha 21: <div class="col-md-10">
Linha 22: @Html.DropDownList("CategoriaId", null, htmlAttributes: new { @class = "form-control" })
Linha 23: @Html.ValidationMessageFor(model => model.CategoriaId, "", new { @class = "text-danger" })
Linha 24: </div>
What is this mistake, and how do I solve it ?
It seems that you are missing a part of your view. Put the whole code.
– victor