我是ASP.NET MVC的新手,正在尝试学习如何通过验证提交表单。我的创建视图运行良好,它列出了带有下拉列表的字段,但是当我提交时,我收到此错误System.ArgumentNullException: Value cannot be null.。我已经阅读了几个小时,无法正常工作。需要帮忙...!这是我公司的课程:[Table("tblCompany")]public class Company{ public int CompanyID { get; set; } [Required] public string Name { get; set; } [Required] [DisplayName("Profile")] public string Type2 { get; set; } public string Industry { get; set; }}HomeController中的动作:public ActionResult CreateCompany() { // Assume these two lists are working fine. The form Dropdownlist do list the options form these lists. ViewBag.CompanyType = GenericMethods.GetGenericPicks2("CompanyType2").OrderBy(e => e.Name); ViewBag.CompanyIndustry = GenericMethods.GetGenericPicks2("CompanyIndustry").OrderBy(e => e.Name); return View(); }[HttpPost]public ActionResult CreateCompany(Company comp) { if (ModelState.IsValid) { Response.Write("Saving to DB"); // code to save to database, redirect to other page return View(comp); } else { return View(comp); } }最后,这是创建视图。该视图工作正常,显示带有选项的下拉列表...,但是当我单击“提交”时,收到了上述错误。
1 回答
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
由于@StephenMuecke已经提出了更改建议,因此您还需要在POST方法中填充ViewBag,以便您可以在视图中使用它。
[HttpPost]
public ActionResult CreateCompany(Company comp)
{
ViewBag.CompanyType = GenericMethods.GetGenericPicks2("CompanyType2").OrderBy(e => e.Name);
ViewBag.CompanyIndustry = GenericMethods.GetGenericPicks2("CompanyIndustry").OrderBy(e => e.Name);
if (ModelState.IsValid)
{
Response.Write("Saving to DB");
// code to save to database, redirect to other page
return View(comp);
}
else
{
return View(comp);
}
}
- 1 回答
- 0 关注
- 143 浏览
添加回答
举报
0/150
提交
取消