c# - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CompanyID' -
this question has answer here:
i've been reading stackoverflow nothing has been able me this. i'm working on position model has selectlists fields couple other models, yet every time try , save error:
"an exception of type 'system.invalidoperationexception' occurred in system.web.mvc.dll not handled in user code
additional information: there no viewdata item of type 'ienumerable' has key 'companyid'."
here's relevant code view:
views/position/create.cshtml
<div class="form-group"> <label class="control-label col-md-2">company</label> <div class="col-md-10"> @html.dropdownlist("companyid", (selectlist)viewbag.allcompanies, new { @class = "form-control" }) </div> </div>
here's relevant code controller:
controllers/positioncontroller.cs
// get: /position/create public actionresult create() { //create query find committees var query = m in db.majors orderby m.major select m; //execute query , store in list list<majors> allmajors = query.tolist(); //convert list select list format needed html selectlist allmajorslist = new selectlist(allmajors, "majorid", "major"); viewbag.allmajors = allmajorslist; //create query find committees var query2 = c in db.companies orderby c.companyname select c; //execute query , store in list list<company> allcompanies = query2.tolist(); //convert list select list format needed html selectlist allcompanieslist = new selectlist(allcompanies, "companyid", "companyname"); viewbag.allcompanies = allcompanieslist; //create query find committees var query3 = in db.industries orderby i.industryname select i; //execute query , store in list list<industry> allindustries = query3.tolist(); //convert list select list format needed html selectlist allindustrieslist = new selectlist(allindustries, "industryid", "industryname"); viewbag.allindustries = allindustrieslist; return view(); } // post: /position/create // protect overposting attacks, please enable specific properties want bind to, // more details see http://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include="positionid,positiontitle,positiontype,positionlocation,positiondeadline,positiondescription")] position position, int[] selectedmajors, int32 companyid, int32 industryid) { //find selected committee company selectedcompany = db.companies.find(companyid); industry selectedindustry = db.industries.find(industryid); //associate committee event position.positioncompany = selectedcompany; position.positionindustry = selectedindustry; if (modelstate.isvalid) { //if there majors add, add them if (selectedmajors != null) { foreach (int majorid in selectedmajors) { majors majortoadd = db.majors.find(majorid); position.applicablemajors.add(majortoadd); } } db.positions.add(position); db.savechanges(); return redirecttoaction("index"); } return view(position); }
i can't seem figure out i'm doing wrong. appreciated!
your problem if (modelstate.isvalid)
false.. , you're not resetting viewbag values when return view post..
if viewbag values null, view in viewdata matches name of property , that's why you're getting error. if comment of code in [httpget] action , return view there, same errors.
you should try moving of code private void..
private void loadviewbag() { //create query find committees var query = m in db.majors orderby m.major select m; //execute query , store in list list<majors> allmajors = query.tolist(); //convert list select list format needed html selectlist allmajorslist = new selectlist(allmajors, "majorid", "major"); viewbag.allmajors = allmajorslist; //create query find committees var query2 = c in db.companies orderby c.companyname select c; //execute query , store in list list<company> allcompanies = query2.tolist(); //convert list select list format needed html selectlist allcompanieslist = new selectlist(allcompanies, "companyid", "companyname"); viewbag.allcompanies = allcompanieslist; //create query find committees var query3 = in db.industries orderby i.industryname select i; //execute query , store in list list<industry> allindustries = query3.tolist(); //convert list select list format needed html selectlist allindustrieslist = new selectlist(allindustries, "industryid", "industryname"); viewbag.allindustries = allindustrieslist; }
then change action to
// get: /position/create public actionresult create() { loadviewbag(); return view(); }
then change post action
// post: /position/create // protect overposting attacks, please enable specific properties want bind to, // more details see http://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include="positionid,positiontitle,positiontype,positionlocation,positiondeadline,positiondescription")] position position, int[] selectedmajors, int32 companyid, int32 industryid) { //find selected committee company selectedcompany = db.companies.find(companyid); industry selectedindustry = db.industries.find(industryid); //associate committee event position.positioncompany = selectedcompany; position.positionindustry = selectedindustry; if (modelstate.isvalid) { //if there majors add, add them if (selectedmajors != null) { foreach (int majorid in selectedmajors) { majors majortoadd = db.majors.find(majorid); position.applicablemajors.add(majortoadd); } } db.positions.add(position); db.savechanges(); return redirecttoaction("index"); } loadviewbag(); return view(position); }
Comments
Post a Comment