Partho Rocko

Partho Rocko

  • NA
  • 10
  • 1.9k

MVC error... please help how to fix it

Aug 22 2016 2:31 AM
The Error Message Is-
(The ViewData item that has the key 'DistrictId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>')
// My Controller is-
//The problem is related to DistrictId
[HttpPost]
[ValidateAntiForgeryToken]
//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
// 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<SelectListItem>' rather than Int....I dont get it why...its the list which is IEnumerable not DistrictId
[Display(Name = "Select District")]
public IList<SelectListItem> 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
@{
ViewBag.Title = "Insert";
}
<script src="~/Views/MobileStore/Scripts/jquery-3.0.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
<h2>Insert</h2>
@using (Html.BeginForm("Insert", "AppForm", FormMethod.Post,
new { @enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ObjectModel</legend>
@Html.DropDownListFor(x => x.DistrictId, Model.DistrictNamesModel , "--Select--", new { @id = "ddlDistrict"});
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Insert")
</div>

Answers (1)