我是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 關注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消