0

Create.cshtml

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

MakaleController.cs

public ActionResult Create()
        {
            ViewBag.KategoriId = new SelectList(db.Kategoris, "KategoriId", "KategoriAdi");
            return View();
        }

but it's say When I make a post request,

The ViewData element with the 'KategoriId' key is of type 'System.Int32', but of type 'IEnumerable '.

How to fix this error, I tried many ways but it did not happen. He says that the value is generally null.

Kategori.cs (!! NOT KATEGORIS, i create with code first)

[Table("Kategori")]
    public partial class Kategori
    {
        public int KategoriId { get; set; }

        [StringLength(50)]
        public string KategoriAdi { get; set; }

        public virtual Makale Makale { get; set; }
    }
xxxecute
  • 194
  • 2
  • 9

1 Answers1

0

Updated Your dropdown definition is not right

it should look like below

@Html.DropDownListFor(m => m.CategoryID, Model.CategoryList, "-Please select-")

you pass null to Dropdownlist, but it is supposed to be at least an empty list You also try to keep dropdownlist data in viewbag. it is supposed to be a List property in the view model. P.S: using native words in variable names are not acceptable in global projects due to readability issues =)

P.S: You can check a similar question and answer from here The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

ilkerkaran
  • 4,214
  • 3
  • 27
  • 42