Error loading page CREATE

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. However, when generating the% with 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>

I get the following error when I try to access it:

  

Server Error in Application '/'.   There is no ViewData item of type 'IEnumerable' that has the 'CategoryId' key.   Description: An unhandled exception occurred during the execution of the current Web request. Examine the stack trace for more information about the error and where it originated in the code.   Exception Details: System.InvalidOperationException: There is no ViewData item of type 'IEnumerable' that has the 'CategoryId' 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 error, and how do I resolve it?

    
asked by anonymous 01.12.2016 / 04:08

1 answer

1

To generate a DropDownList it is neccessary to have a collection of something to be listed. You can not pass null in this case:

@Html.DropDownList("CategoriaId", null, htmlAttributes: new { @class = "form-control" })

Here you say:

  

Create a DropDownList, with ID in the "CategoryId" HTML, using this null list

Instead of this null , pass a category enumeration, which will no longer have this problem.

    
01.12.2016 / 08:52