隐藏输入 "empty string" vs "null" Javascript, VB
hidden input "empty string" vs "null" Javascript, VB
我试图 JSON 反序列化 VB 中的一个集合,如下所示。
Dim items = JsonConvert.DeserializeAnonymousType(Page.Request.Params("Items"), New List(Of ItemDto))
反序列化出现问题,字符串 "value" 不能为空。
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: value
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
集合 "Items" 存储在 <asp:HiddenField runat="server" ClientIDMode="Static" ID="Items" />
中,转换为 <input type="hidden"....>
但是,如果我在运行之前 $("#Items').val(null);
如果没有项目,那么它会正常工作。
问题来了,为什么$("#Items").val();
在我完成$("#Items").val(null);
之前显示为“”,在完成之后显示为“”,有无明显区别?像零宽度 space?
我不知道为什么将集合设置为 "null" 使代码工作。
谢谢。
$("#Items").val();
从不 returns NULL,因此返回空字符串,当您这样做时
$("#Items').val(null);
它将值设置为“”或空字符串而不是 null
所以不会抛出下面的异常
System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentNullException: Value cannot be null.
因为您的 JQuery 将该值设置为“”而不是 NULL,因此不会引发 ASP.NET 异常
我试图 JSON 反序列化 VB 中的一个集合,如下所示。
Dim items = JsonConvert.DeserializeAnonymousType(Page.Request.Params("Items"), New List(Of ItemDto))
反序列化出现问题,字符串 "value" 不能为空。
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: value
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
集合 "Items" 存储在 <asp:HiddenField runat="server" ClientIDMode="Static" ID="Items" />
中,转换为 <input type="hidden"....>
但是,如果我在运行之前 $("#Items').val(null);
如果没有项目,那么它会正常工作。
问题来了,为什么$("#Items").val();
在我完成$("#Items").val(null);
之前显示为“”,在完成之后显示为“”,有无明显区别?像零宽度 space?
我不知道为什么将集合设置为 "null" 使代码工作。
谢谢。
$("#Items").val();
从不 returns NULL,因此返回空字符串,当您这样做时
$("#Items').val(null);
它将值设置为“”或空字符串而不是 null
所以不会抛出下面的异常
System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentNullException: Value cannot be null.
因为您的 JQuery 将该值设置为“”而不是 NULL,因此不会引发 ASP.NET 异常