The Viewdata item that has the key 'Categoryid' is of type 'System.Int32' but must be of type 'Ienumerable<Selectlistitem>'

Asked

Viewed 120 times

0

When I Create a New Subcategory I Have the Following Error:

The Viewdata item that has the key 'Categoryid' is of type 'System.Int32' but must be of type 'Ienumerable'.

Classes that are using:

    public class SubCategory
    {
        [Key]
        [Display(Name = "SubCategoria Id")]
        public int SubCategoryId { get; set; }

        [MaxLength(50, ErrorMessage = "O campo Sub-Categoria recebe no máximo 50 caracteres")]
        [Index("SubCategory_Category_Description_Index",2, IsUnique = true)]
        [Required(ErrorMessage = "O campo {0} é requerido!!")]
        [Display(Name = "Sub-Categoria")]
        public string Description { get; set; }

        [Required(ErrorMessage = "O campo Categoria é requerido!!")]
        [Index("SubCategory_Category_Description_Index", 1, IsUnique = true)]
        [Range(1, double.MaxValue, ErrorMessage = "Selecione a Categoria")]
        [Display(Name = "Categoria")]
        public int CategoryId { get; set; }

        public virtual Category Category { get; set; }

        public virtual ICollection<Classification> Classifications { get; set; }

        public virtual ICollection<Medicament> Medicaments { get; set; }
    }



    // GET: SubCategories/Create
        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(), "CategoryId", "Description");
            return View();
        }

        // POST: SubCategories/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(SubCategory subCategory)
        {
            if (ModelState.IsValid)
            {
                db.SubCategories.Add(subCategory);
                try
                {
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch (System.Exception ex)
                {

                    if (ex.InnerException != null &&
                        ex.InnerException.InnerException != null &&
                        ex.InnerException.InnerException.Message.Contains("_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "Não é possível inserir duas Subcategorias com o mesmo nome!");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }


                    return View(subCategory);
                }
            }
            ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(), "CategoryId", "Description");

            return View(subCategory);
        }

    @model SIG_HP.Models.SubCategory

        @{
        ViewBag.Title = "Create";
    }
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SubCategory</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m => m.CategoryId, (SelectList)ViewBag.CargoId)
                @Html.ValidationMessageFor(model => model.CategoryId, "", 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>
  • Please if anyone can help

  • In your controller you declare the ViewBag.CategoryId and in your view tries to mount the dropdown with (SelectList)ViewBag.CargoId?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.