c# - 系统参数异常 mvc .net

标签 c# asp.net asp.net-mvc asp.net-mvc-4 datetime

我在查询字符串中传递参数。当我将链接悬停在浏览器中时,它会正确显示查询字符串中的键值对。当我单击链接时,首先,断点不会在特定操作处触发,其次,我遇到以下异常:

The parameters dictionary contains a null entry for parameter 'endDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult GetAllExpenses(System.String, System.String, System.DateTime, System.DateTime)' in 'GPI.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

我知道它要求 endDate 参数不为空,但值是在查询字符串的变量中传递的。我没明白。请帮助我理解这一点。

最初我试过这个:-

/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)

我再次遇到同样的异常。

这是浏览器中链接的图像。可以看到变量的所有值都在那里

enter image description here

图片中的链接对应于第一个框,即第 1 季度和借方

我的部分 View 中有以下这些变量,我将它们传递给查询字符串中它们各自的链接:

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);

最佳答案

因为您无法真正控制发送到您的 Controller 的数据,您应该使用 DateTime? 将这些字段标记为可为空,然后检查强制值是否存在缺失值和相应地输出,无论是错误还是成功执行的操作。

关于c# - 系统参数异常 mvc .net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650067/

相关文章:

c# - 如何打印 WebView 内容 C# UWP Win 10

asp.net-mvc - 如何在 aspnetusers 表中获取自定义字段?

asp.net-mvc - VS2012 Enable NuGet Package Restore 消失,丢失

c# - 如何根据登录成功在新窗口/选项卡或同一窗口/选项卡中打开页面?

ASP.NET 配置数据源没有返回任何内容?

c# - RDLC报告中我的日期时间数据字段中的错误

asp.net-mvc - 检查用户是否在用户控件中登录 Asp.net MVC

c# - C# 默认使用的 RSA 算法是什么,它在 Crypto++ 中的合适参数是什么?

c# - textBox 在回发时丢失值?

C# 使按钮在两个条件为真后可见,具有可绑定(bind)属性