Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

i'm new to MVC.

I have creating one application in mvc.

Where i have create view, in which i have bind department dropdownlistfor and successfully i had bind.

But, after inserting all the values in the create form, when i clicked submit button.
i got this error:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Dept.DepartmentID'


What I have tried:

Business layer
namespace BusinessLayer
{
    public class Workshop_BL
    {
        [Key]
        public  int RecID { get; set; }
        public  string AssetNo { get; set; }
        public Departments Dept { get; set; }
        public  string Location { get; set; }
        public  decimal Cost { get; set; }
        public  string Description { get; set; }
        public  DateTime WoDate { get; set; }
        public  string WoNo { get; set; }
        public  DateTime? ExpDate { get; set; }
        public  DateTime InputDate { get; set; }
        public  string WStatus { get; set; }
        public  string Remarks { get; set; }
        public  int? Manpower { get; set; }
        public  int? EstDays { get; set; }
        public  int? EstHours { get; set; }
        public  DateTime?  PartsRecvDate { get; set; }

    }

    public class Departments
    {
        [Key]
        public int DepartmentID { get; set; }
        public string DepratmentName { get; set; }
    }


}


Fetching Data from Sql server
public IEnumerable<Departments> Dept
{
    get
    {
        List<Departments> deptlist = new List<Departments>();
        DataTable dt = DA.GetData("SP_GETALL_DEPARTMENTS");
        foreach(DataRow dr in dt.Rows)
        {
            Departments dpl = new Departments();
            dpl.DepartmentID = Convert.ToInt32(dr["DepartmentID"].ToString());
            dpl.DepratmentName = dr["Department_Name"].ToString();
            deptlist.Add(dpl);
        }

        return deptlist;
    }
}


Controller
[HttpGet]
public ActionResult Create()
{
    WorkshopActivity_Bl objWs = new WorkshopActivity_Bl();
    List<Departments> dpl = objWs.Dept.ToList();
    IEnumerable<SelectListItem> items = objWs.Dept.Select(c => new SelectListItem { Value = c.DepartmentID.ToString(), Text = c.DepratmentName });
    ViewBag.Dept = items;
    return View("Create");
}

[HttpPost]
public ActionResult Create(Workshop_BL bl)
{
    if (ModelState.IsValid)
    {
        WorkshopActivity_Bl objwl = new WorkshopActivity_Bl();
        objwl.InsertWorkshopActivity(bl);
        return RedirectToAction("DetailsList");
    }
    return View("Create");
}


Create View
        <div class="editor-label">
            @Html.LabelFor(Model => Model.Dept)
</div>
        <div class="editor-field">
                        @Html.DropDownListFor(Model => Model.Dept, (IEnumerable<SelectListItem>)ViewBag.Dept, "Select")
                        @Html.ValidationMessageFor(Model => Model.Dept)
        </div>


I dont have errors in get method.

but i'm getting error in post method.


So, please guys, help me.


thanks
Posted
Updated 22-May-16 0:22am
v2
Comments
Karthik_Mahalingam 21-May-16 9:08am    
is the model valid?
F-ES Sitecore 22-May-16 8:56am    
When you have an error always say what line the error is on. You get that error when you try and treat a list of items as a single item. So if dept is IEnumerable<SomeClass> you use it like this

foreach (SomeClass x in dept)
{
x.SomeProp;
}

You'll get the error you are seeing if you write code like

dept.SomeProp

1 solution

Hi Abdul,

Add the Dropdown binding code in Post Method as well.

C#
WorkshopActivity_Bl objWs = new WorkshopActivity_Bl();
    List<Departments> dpl = objWs.Dept.ToList();
    IEnumerable<SelectListItem> items = objWs.Dept.Select(c => new SelectListItem { Value = c.DepartmentID.ToString(), Text = c.DepratmentName });
    ViewBag.Dept = items;


Once you add these lines in post method the code should work.

ViewData and ViewBag can be used only in the Single Request. The data will be lost in the PostBack. So you need to set it again.

Also Change the View Code.

You cannot write Model.Dept.DepatmentID to the Model Property Name.

Like Model.Dept.FKDeptID.

This Should help.
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 21-May-16 6:26am    
5
abdul subhan mohammed 21-May-16 6:35am    
still i'm getting the same error..

updated view code

<div class="editor-field">
@Html.DropDownListFor(Model => Model.Dept, (IEnumerable<SelectListItem>)ViewBag.Dept, "Select")
@Html.ValidationMessageFor(Model => Model.Dept)
</div>

and change dept type to int.. in model

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900