0

Using Angular and ASP.NET MVC, I'm passing in a JSON to my backend in ASP.NET. For the sake of example, let's just say I pass in a parameter that contains:

[{"id":1,"logo":""}, {"id":2,"logo":""}, {"id":3,"logo":""}]

in my backend I take this in as object:

public JsonResult SaveHeadlines(List<SomeObject> objectHere)

objectHere's properties of "logo" all contain null instead of an empty string.

Is there any way around this? I'm not sure if this is an Angular to ASP.NET bug or something...

EDIT:

//Serializing     
List<Headlines> headlines = ServiceStack.Text.JsonSerializer.DeserializeFromString<List<Headlines>>(json);

return Json(headlines);
PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
user1189352
  • 3,628
  • 12
  • 50
  • 90
  • What type do you have for `logo`? – Greg Sep 16 '15 at 22:30
  • @Greg the property is a string – user1189352 Sep 16 '15 at 22:31
  • How are you deserializing your Json into C#? – Greg Sep 16 '15 at 22:33
  • To narrow down the culprit, look at the HTTP request in your developer tools; is the JSON formated as expected in the request, or is that where the `null` values come from? – Jacob Sep 16 '15 at 22:35
  • @Greg edited OP for you – user1189352 Sep 16 '15 at 22:36
  • @Jacob i'll double check. but i did a console.log of the JSON object and it's still empty blanks just before I send it to my back end. Somehow converting it into List is creating it as null – user1189352 Sep 16 '15 at 22:37
  • @Jacob I looked at the Network --> Headers and it's definitely an empty string – user1189352 Sep 16 '15 at 22:44
  • Removed Angular tag and added `ServiceStack`, which appears to have its own serializer; maybe someone familiar with `ServiceStack` will find this. – Jacob Sep 16 '15 at 23:01
  • @Jacob i think it's more an ASP.NET thing.... as for now I'm just going to write a method that converts all nulls into empty strings.. I can't think of any better solution for now. I was just hoping there was a more elegant solution – user1189352 Sep 16 '15 at 23:13
  • ASP.NET's model binder does not convert empty strings to nulls; ServiceStack may be introducing something odd since it has its own deserializer. – Jacob Sep 16 '15 at 23:14
  • Could this question and answer help you? http://stackoverflow.com/questions/23830206/json-convert-empty-string-instead-of-null – Rick Sep 17 '15 at 11:39
  • @Rick i believe that does, thank you! – user1189352 Sep 17 '15 at 16:27
  • @Rick that using NewtonSoft for JSON rather than ServiceStack which i'm currently using though but it will help future people who run into the same problem. – user1189352 Sep 17 '15 at 16:29

0 Answers0