[英]System argument exception mvc .net
我在查询字符串中传递参数。 当我在浏览器中悬停链接时,它可以正确显示查询字符串中的键值对。 当我单击链接时,首先,不会在特定操作上触发断点,其次,我得到了以下异常:
参数字典包含方法'System.Web.Mvc.ActionResult GetAllExpenses(System.String,System.String,System.DateTime,System.DateTime)的非空类型'System.DateTime'的参数'endDate'的空条目在“ GPI.Controllers.HomeController”中。 可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。
我了解它要求endDate
参数不为null,但值正在查询字符串的变量中传递。 我没听懂 请帮助我理解这一点。
最初我尝试过:
/Home/GetAllExpenses?userId=@ViewBag.Id&expType=debit&startDate=@quarterThreeStartDate&endDate=@quarterThreeEndDate
并有同样的问题
之后,我尝试了这个:
/Home/GetAllExpenses?userId=@HttpUtility.UrlEncode(ViewBag.Id)&expType=debit&startDate=@HttpUtility.UrlEncode(quarterThreeStartDate)&endDate=@HttpUtility.UrlEncode(quarterThreeEndDate)
我再次得到同样的例外。
这是浏览器中链接的图像。 您可以看到所有变量值都在那里
图片中的链接对应于第一个方框,即四分之一和借方
我在局部视图中具有以下变量,这些变量已传递给querystring中它们各自的链接:
var quarterOneStartDate = new DateTime(ViewBag.Year, 1, 1);
var quarterTwoStartDate = new DateTime(ViewBag.Year, 4, 1);
var quarterThreeStartDate = new DateTime(ViewBag.Year, 7, 1);
var quarterFourStartDate = new DateTime(ViewBag.Year, 10, 1);
var quarterOneEndDate = new DateTime(ViewBag.Year, 3, 31);
var quarterTwoEndDate = new DateTime(ViewBag.Year, 6, 30);
var quarterThreeEndDate = new DateTime(ViewBag.Year, 9, 30);
var quarterFourEndDate = new DateTime(ViewBag.Year, 12, 31);
因为您不能真正控制发送到控制器的数据,所以应该使用DateTime?
将这些字段标记为可空的DateTime?
并且无论是错误还是成功执行的操作,都必须检查必需值以获取缺失值并相应地输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.