Viewdata 的 Javascript 错误
我有一个链接到一个控制器,该控制器呈现包含此 javascript 的页面。
$(document).ready(function () {
var creditCards = '<%:(ViewData["CreditCards"])%>';
alert(creditCards);
if (creditCards != null) {
var CreditCardViewData = '<%:((SelectList)ViewData["CreditCards"]).Count() %>';
....
我正在测试的情况是 viewData creditCards 为空。我调试它,它到达 varcreditCards = '<%:(ViewData["CreditCards"])%>';
行,当我单击单步进入时,它会一直跳到 <代码>var CreditCardViewData = '<%:((SelectList)ViewData["CreditCards"]).Count() %>';. (注意:当ViewData CreditCards不为空时,该页面可以正常工作)
跳转后,立即给出错误:“argumentnullException:值不能为空。 参数名称:源。”
我如何防止出现此错误。谢谢
I have a link that goes to a controller that renders a page that includes this javascript.
$(document).ready(function () {
var creditCards = '<%:(ViewData["CreditCards"])%>';
alert(creditCards);
if (creditCards != null) {
var CreditCardViewData = '<%:((SelectList)ViewData["CreditCards"]).Count() %>';
....
The situation I am testing is when the viewData creditCards is null. I debug it and it gets to the var creditCards = '<%:(ViewData["CreditCards"])%>';
line and when i click step into it jumps all the way to var CreditCardViewData = '<%:((SelectList)ViewData["CreditCards"]).Count() %>';
. (Note: this page works fine when the ViewData CreditCards is not null)
After it jumps, it instantly gives the error: "argumentnullexception: Value cannot be null.
Parameter name: source."
How do i prevent this error from showing up. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将代码更改为以下内容,以确保仅在 ViewData["CreditCards"] 不为 null 时调用 Count()。
Count() 方法检查参数是否为 null,如果检测到无效 (null) 参数,则抛出 ArgumentNullException。
Try changing your code to the following to ensure that the Count() is only called if ViewData["CreditCards"] is not null.
The Count() method checks arguments for null and then throws the ArgumentNullException if it detects an invalid (null) argument.