Garima Bansal

Garima Bansal

  • 1.1k
  • 551
  • 29k

ViewData item that has the key 'code' is of type 'SystemInt32'

Dec 5 2023 5:11 AM

when i run my application it gives me this error on dropdownlist

The ViewData item that has the key 'talukcode' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

Below is the view code :

@Html.DropDownListFor(model => model.talukcode, ViewBag.talukname as IEnumerable<SelectListItem>, "-- Select Tehsil --", new { @class = "form-control", @onchange = "FillCommune()", @id = "talukcode" })
                                @Html.ValidationMessageFor(model => model.talukcode, "", new { @class = "text-danger" })

Below is the Controller code :

public int VillageAdd(MasterModel model)
{
    int result = 0;
    int al_talukcd = 0, al_communecd = 0;
    string al_village = string.Empty;

    try
    {
        using (var context = new PPAEntities())
        {
            var datavillage = new ppavillage { regioncode = model.regioncode, talukcode = model.talukcode, villagecode = model.villagecode, villagename = model.villagename };

            if (model.villagecode == 0)
            {
                var name_alexits = context.ppavillages.Where(x => x.talukcode == model.talukcode && x.villagename == model.villagename).FirstOrDefault();
                if (name_alexits != null)
                {
                    al_talukcd = name_alexits.talukcode;
                    //al_communecd = name_alexits.communecode;
                    //al_village = name_alexits.villagename;
                    al_village = name_alexits.villagename;
                }


                if (al_village.Trim().ToUpper() != model.villagename.Trim().ToUpper())
                {

                    var MaxValue = (from c in context.ppavillages
                                    where c.regioncode == model.regioncode //&& c.talukcode == model.talukcode
                                    select c.villagecode).Max();
                    model.villagecode = MaxValue + 1;
                    ppavillage villagemst = new ppavillage();
                    villagemst.regioncode = model.regioncode;
                    villagemst.talukcode = model.talukcode;
                    villagemst.communecode = model.communecode;
                    villagemst.villagecode = model.villagecode;
                    villagemst.villagename = model.villagename;
                    villagemst.villagenamet = null;
                    villagemst.univillagename = null;
                    villagemst.urcodecn = "0";
                    villagemst.urcodet = "0";
                    context.ppavillages.Add(villagemst);
                    context.SaveChanges();
                    result = 1;
                }
                else
                {
                    result = 2;
                }

            }
            else
            {
                using (var contextdb = new PEntities())
                {
                    contextdb.ppavillages.Attach(datavillage);
                    contextdb.Entry(datavillage).Property(x => x.villagename).IsModified = true;
                    contextdb.SaveChanges();
                    result = 3;
                }
            }
        }

    }
    catch (Exception ex)
    {

    }
    return result;
}

How can i solve this error?


Answers (7)