Asp.net mvc 3 ArgumentNull异常由MVC3中的用户代码处理
我的应用程序是用C#.Net编码的Asp.NETMVC3。 我有包含DropDownList的视图。这些DropDownList是从某个主视图填充的。 下面是我的控制器代码Asp.net mvc 3 ArgumentNull异常由MVC3中的用户代码处理,asp.net-mvc-3,Asp.net Mvc 3,我的应用程序是用C#.Net编码的Asp.NETMVC3。 我有包含DropDownList的视图。这些DropDownList是从某个主视图填充的。 下面是我的控制器代码 [HttpPost] public ActionResult TestCreate(MainMaster Testmaster) { var recExists= from c in db.MainMaster
[HttpPost]
public ActionResult TestCreate(MainMaster Testmaster)
{
var recExists= from c in db.MainMaster
where c.CityName == Testmaster.CityName && c.StateID==Testmaster.StateID
select c;
if (nid.Count()>0)
{
ModelState.AddModelError("", "This City Already Exists");
}
if (ModelState.IsValid)
{
db.MainMaster.Add(Testmaster);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(Testmaster);
}
}
只有当我尝试输入重复记录时,我才会收到错误。示例:如果已经有一个名为马哈拉施特拉邦孟买的城市,而我仍然尝试输入马哈拉施特拉邦孟买的记录。那么我无法显示错误消息ModelState.AddModelError(“,”这个城市已经存在”)代码>。我得到的是系统。ArgumentNullException:值不能为空。
错误
下面是我的查看代码。
@Html.DropDownList("CountryID", new SelectList(ViewBag.CountryIDies as System.Collections.IEnumerable, "CountryID", "CountryName"), new { id="Country" })
这是我的下拉列表,我在同一行上出错。
当我在那些不包含DropDownList的视图中使用它时,这段代码工作得很好。
我已经调试并检查了Testmaster中的值,它给出了所选组合的正确值。
我还检查了将值传递给DropDownList的ViewBag属性。它将所有状态传递给DropDownList。当编译器到达
ModelState.AddModelError("", "This City Already Exists");
因此,我的@Html.DropDownList(“CountryID”、新的SelectList(ViewBag.CountryIDies as System.Collections.IEnumerable、“CountryID”、“CountryName”)、新的{id=“Country”}中没有任何值。
为了克服这一点,我补充道
ViewBag.CountryID = new SelectList(db.CountryMasters, "CountryID", "CountryName");
问题得到了解决。当编译器到达
ModelState.AddModelError("", "This City Already Exists");
因此,我的@Html.DropDownList(“CountryID”、新的SelectList(ViewBag.CountryIDies as System.Collections.IEnumerable、“CountryID”、“CountryName”)、新的{id=“Country”}中没有任何值。
为了克服这一点,我补充道
ViewBag.CountryID = new SelectList(db.CountryMasters, "CountryID", "CountryName");
问题得到了解决