Skip to content

MVC error... please help how to fix it

Asked Aug 21, 2016, 10:31 PM EDT1 answer
Share
LinkedInX
The Error Message Is-
(The ViewData item that has the key 'DistrictId' is of type 'System.Int32' but must be of type 'IEnumerable')
// My Controller is-
//The problem is related to DistrictId
[HttpPost]
[ValidateAntiForgeryToken]
Code
        //STORING THE DATA into the database
             public ActionResult Insert(ObjectModel OM, HttpPostedFileBase Photo, HttpPostedFileBase Resume, HttpPostedFileBase IdProof)
        {
            if (ModelState.IsValid)  //checking model is valid or not
            {
DB objDB = new DB(); //calling class DBdata
C#
                // i am showing only DistrictId and related details , I deleted rest of the code which is not useful for here ...

                    int DistrictIdInt = Convert.ToInt32(OM.DistrictId);
                    OM.DistrictId = DistrictIdInt;
                    int result = objDB.InsertEmployementApplication(OM); // passing Value to DBClass from model
                //I think the problem is occured after THIS SECTION
                    ModelState.Clear(); //clearing model
                    return View(OM);
                }

            }

            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                ModelState.AddModelError("", "Error in saving data");
                return View();
            }


            // My Model is-

          // The Data is getting saved in my database but this error is popping up again and again.
        {

         //iT ASKS for districtId to be 'IEnumerable' rather than Int....I dont get it why...its the list which is IEnumerable not DistrictId
[Display(Name = "Select District")]
C#
        public IList DistrictNamesModel { get; set; }

        [Required(ErrorMessage = "Please Select District")]
        public int DistrictId { get; set; }
    }

             //My View is-
             //the problem is in dropDownlistFor Tag..
@model MVCtrendsetters.Models.ObjectModel
Code
       @{
       ViewBag.Title = "Insert";
      }

Insert

@using (Html.BeginForm("Insert", "AppForm", FormMethod.Post,
TypeScript
                        new { @enctype = "multipart/form-data" }))

         {
         @Html.AntiForgeryToken()
         @Html.ValidationSummary(true)
ObjectModel
@Html.DropDownListFor(x => x.DistrictId, Model.DistrictNamesModel , "--Select--", new { @id = "ddlDistrict"});

}
@Html.ActionLink("Back to List", "Insert")

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    None knows how to solve this error?really? OMG Is there none on CsharpCorner who knows how to solve this?
    0