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
Below is the view code :
@Html.DropDownListFor(model => model.talukcode, ViewBag.talukname as IEnumerable, "-- 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?
Jignesh KumarPosted Dec 6, 2023, 5:53 AM
Hello,
Please check this one,
Ramesh PalanivelPosted Dec 5, 2023, 6:48 AM
Hi Garima,
I cannot see any where you binded value for ViewBag.talukname. Try below code
Garima BansalPosted Dec 5, 2023, 6:34 AM
Ramesh Palanivel Sir,
Below is controller code can you please tell me where i have to bind it
Ramesh PalanivelPosted Dec 5, 2023, 6:29 AM
Gariam,
what you binding on your ViewBag,TalukName? As Jayaraj share sample code, it should be looks like,
Garima BansalPosted Dec 5, 2023, 6:18 AM
In model,code is below
public int talukcode { get; set; }
public string talukname { get; set; }
Jayraj ChhayaPosted Dec 5, 2023, 6:07 AM
The error message suggests that the 'talukcode' ViewData item is expected to be of type 'IEnumerable', but it is currently of type 'System.Int32'. This error typically occurs when there is a mismatch between the expected data type and the actual data type being passed to the dropdown list.
To resolve this error, you need to ensure that the 'talukcode' ViewData item is of the correct type. In the provided code snippet, it seems that the 'talukcode' ViewData item is being set as an integer value instead of a collection of SelectListItem objects.
To fix this issue, you should modify the code in your controller to populate the 'talukcode' ViewData item with a collection of SelectListItem objects. Here's an example of how you can achieve this:
In your view code, you can then use the 'talukcode' ViewData item as follows:
By ensuring that the 'talukcode' ViewData item is of the correct type, the error should be resolved, and the dropdown list should render correctly in your MVC application.
Ramesh PalanivelPosted Dec 5, 2023, 6:06 AM
Hi Garima,
The problem is on your code,
@Html.DropDownListFor(model => model.talukcode, ViewBag.talukname as IEnumerable, "-- Select Tehsil --", new { @class = "form-control", @onchange = "FillCommune()", @id = "talukcode" })
@Html.ValidationMessageFor(model => model.talukcode, "", new { @class = "text-danger" })
ViewBag.talukname not able to assign type as IEnumerable . Check your code where you bind Talukname viewbag.