我一直在阅读StackOverflow但是没有什么能真正帮助我解决这个问题。我正在研究一个位置模型,其中包含来自其他几个模型的字段的SelectLists,但每次我尝试保存时都会收到错误:
“System.Web.Mvc.dll中发生了'System.InvalidOperationException'类型的异常,但未在用户代码中处理
附加信息:没有类型为“IEnumerable”的ViewData项具有“CompanyID”键。“
以下是我认为的相关代码:
查看/位置/ 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>
以下是我的控制器的相关代码:
控制器/ PositionController.cs
// GET: /Position/Create
public ActionResult Create()
{
//create query to find all committees
var query = from m in db.Majors
orderby m.Major
select m;
//execute query and store in list
List<Majors> allMajors = query.ToList();
//convert list to select list format needed for HTML
SelectList allMajorsList = new SelectList(allMajors, "MajorID", "Major");
ViewBag.AllMajors = allMajorsList;
//create query to find all committees
var query2 = from c in db.Companies
orderby c.CompanyName
select c;
//execute query and store in list
List<Company> allCompanies = query2.ToList();
//convert list to select list format needed for HTML
SelectList allCompaniesList = new SelectList(allCompanies, "CompanyID", "CompanyName");
ViewBag.AllCompanies = allCompaniesList;
//create query to find all committees
var query3 = from i in db.Industries
orderby i.IndustryName
select i;
//execute query and store in list
List<Industry> allIndustries = query3.ToList();
//convert list to select list format needed for HTML
SelectList allIndustriesList = new SelectList(allIndustries, "IndustryID", "IndustryName");
ViewBag.AllIndustries = allIndustriesList;
return View();
}
// POST: /Position/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// 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 with event
position.PositionCompany = SelectedCompany;
position.PositionIndustry = SelectedIndustry;
if (ModelState.IsValid)
{
//if there are majors to 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);
}
我似乎无法弄清楚我做错了什么。任何帮助将不胜感激!
答案 0 :(得分:2)
您的问题是settings.py
很可能是错误的...当您从POST返回View时,您没有重置ViewBag值。
如果ViewBag值为null,则视图将在ViewData中查找与属性名称匹配的内容,这就是您收到该错误的原因。如果您对[HttpGet]操作中的所有代码进行注释并只返回View,则会出现相同的错误。
您应该尝试将所有这些代码移动到私有空间中。
if (ModelState.IsValid)
然后将您的GET操作更改为
private void LoadViewBag()
{
//create query to find all committees
var query = from m in db.Majors
orderby m.Major
select m;
//execute query and store in list
List<Majors> allMajors = query.ToList();
//convert list to select list format needed for HTML
SelectList allMajorsList = new SelectList(allMajors, "MajorID", "Major");
ViewBag.AllMajors = allMajorsList;
//create query to find all committees
var query2 = from c in db.Companies
orderby c.CompanyName
select c;
//execute query and store in list
List<Company> allCompanies = query2.ToList();
//convert list to select list format needed for HTML
SelectList allCompaniesList = new SelectList(allCompanies, "CompanyID", "CompanyName");
ViewBag.AllCompanies = allCompaniesList;
//create query to find all committees
var query3 = from i in db.Industries
orderby i.IndustryName
select i;
//execute query and store in list
List<Industry> allIndustries = query3.ToList();
//convert list to select list format needed for HTML
SelectList allIndustriesList = new SelectList(allIndustries, "IndustryID", "IndustryName");
ViewBag.AllIndustries = allIndustriesList;
}
然后将您的发布操作更改为
// GET: /Position/Create
public ActionResult Create()
{
LoadViewBag();
return View();
}