MerlynCooper
MerlynCooper

Reputation: 74

hidden input "empty string" vs "null" Javascript, VB

I was attempting to JSON deserialize a collection in VB, as seen below.

Dim items = JsonConvert.DeserializeAnonymousType(Page.Request.Params("Items"), New List(Of ItemDto))

There was an issue with the deserialization, the string "value" could not be null.

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)

The collection "Items" was stored in a <asp:HiddenField runat="server" ClientIDMode="Static" ID="Items" /> which translates to <input type="hidden"....>

however, if I did $("#Items').val(null); before it runs if there are no items and then it's working correctly.

The question is, why does $("#Items").val(); show as "" before and "" after I've done $("#Items").val(null); and is there an invisible difference? like a zero width space?

I don't know why setting the collection to "null" has made the code work.

Thanks.

Upvotes: 0

Views: 737

Answers (1)

Pream
Pream

Reputation: 537

$("#Items").val();

never returns NULL and so empty string is returned and when you do

$("#Items').val(null);

it sets the value to "" or empty string and not null

so the following exception will not be thrown

System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentNullException: Value cannot be null.

because the value was set to "" by your JQuery and not NULL thus not raising the ASP.NET exception

Upvotes: 1

Related Questions