Error deserializing JSON to a class with IEnumerable - asp.net-web-api

I need to return a collection with nested optional collections from my WebAPI and convert it back to objects in my Web UI, however I'm getting the following error:
Cannot create and populate list type System.Linq.IQueryable`1[MyNamespace.MyClass].
I'm using Json.Net.
Here is a sample of my code:
public class ClassA
{
public Int64 Id { get; set; }
public String Description { get; set; }
public IEnumerable<ClassB> { get; set; }
}
public class ClassB
{
public Int64 Id { get; set; }
public String Description { get; set; }
}
I've saw some questions here in stackoverflow, but I don't have access to serialization options (it is handled internally by our library).
UPDATE
I've forgot to mention that ClassA is returned as an IQueryable in my Web API.

Changing the return type of my Web API method to IEnumerable instead of IQueryable solved the problem.

Related

Is it possible to query nested objects in Realm with Xamarin?

When accessing a nested object with the code below the following exception appears:
System.NotSupportedException: The left-hand side of the Equal operator must be a direct access to a
persisted property in Realm.
However in Objective C it seems that you can somehow query nested objects. Is it planned or are there are solutions to query nested objects in xamarin?
Realm.All<Person>().Where(p => p.Name.Firstname == "Test");
public class Person : RealmObject
{
public string Town { get; set; }
public PersonName Name { get; set; }
}
public class PersonName : RealmObject
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}
This is a planned feature. You can keep track of the GitHub issue.

WebAPI AND Odata V4 issue: ComplexType containing Entities

I am getting following error while adding relationship inside the complex type. How can I fix this issue. I binged and read that it was issue with OData V3 but not in OData V4.
The complex type 'Microsoft.OneIM.ActiveIncident.Contracts.IncidentImpact' refers to the entity type 'Microsoft.OneIM.ActiveIncident.Contracts.ImpactedService' through the property 'ImpactedServices'.
at System.Web.OData.Builder.ODataConventionModelBuilder.MapComplexType(ComplexTypeConfiguration complexType)
at System.Web.OData.Builder.ODataConventionModelBuilder.MapType(StructuralTypeConfiguration edmType)
at System.Web.OData.Builder.ODataConventionModelBuilder.AddComplexType(Type type)
at System.Web.OData.Builder.ODataConventionModelBuilder.ReconfigureEntityTypesAsComplexType(EntityTypeConfiguration[] misconfiguredEntityTypes)
at System.Web.OData.Builder.ODataConventionModelBuilder.RediscoverComplexTypes()
at System.Web.OData.Builder.ODataConventionModelBuilder.GetEdmModel()
at Microsoft.OneIM.ActiveIncident.Service.ModelBuilder.BuildIncidentModels() in c:\OneIM\EngSys\OneIM\ActiveIncident\src\Product\Service\Models\ModelBuilder.cs:line 42
at Microsoft.OneIM.ActiveIncident.Service.WebApiConfig.Register(HttpConfiguration config) in c:\OneIM\EngSys\OneIM\ActiveIncident\src\Product\Service\App_Start\WebApiConfig.cs:line 22
at Microsoft.OneIM.ActiveIncident.ServiceHost.ApiStartup.Configuration(IAppBuilder appBuilder) in c:\OneIM\EngSys\OneIM\ActiveIncident\src\Product\ServiceHost\ApiStartup.cs:line 27
My model looks as below
public class Incident
{
public IncidentImpact Impact { get; set; }
}
[ComplexType]
public class IncidentImpact
{
public bool IsCustomerImpacting { get; set; }
public string SupportTicketId { get; set; }
public ICollection<ImpactedService> ImpactedServices { get; set; }
}
public class ImpactedService
{
public long Id { get; set; }
public long IncidentId { get; set; }
public Incident Incident { get; set; }
public long ServiceId { get; set; }
}
Although OData protocol V4 support complex type contains entity as navigation property, both of OData Lib and WebAPI OData do not implementation this feature now.
You have to set a key property by [key] attribute or in the model builder as
builder.EntitySet<Type>("Types").EntityType.HasKey(t => t.KeyProperty);
Hope it helps.

Delta<T> in PATCH actions not tracking primitive types

I'm using Delta for patching an entity as outlined in "Partial Updates(PATCH request)" section outlined here
I have the following ProductDTO:
public class ProductDTO
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[UIHint("Date")]
[DataType(DataType.Date)]
public DateTime? ModifiedOn { get; set; }
public int Price { get; set; }
}
And the following action method defined:
public HttpResponseMessage Patch(int id, Delta<ProductDTO> delta)
{
return Request.CreateResponse(HttpStatusCode.NoContent);
}
If I pass in the following JSON (via Fiddler using the PATCH verb)
{"ID":1,"Name":"test","Price":"1000"}
The "delta" in the action method contains only the "Name" property and not the ID and Price properties. It appears that the delta is not containing values of types "int","decimal" and primitive types in general.
What am I missing here?
TIA
EDIT: Here is a link to a modded version of Delta that includes support for primitive JSON data types. Comments appreciated

How to update a property of an abstract with an inheriting/using a subblass in MVC

I have an abstract class
public abstract class Member
{
public string ID { get; set; }
public string Username { get; set; }
public int MemberType { get; set; }
public abstract string MemberName { get; set; }
public int Status { get; set; }
}
public class Person : Member
{
public string LastName { get; set; }
public string FirstName{ get; set; }
}
public class Business : Member
{
public string BusinessName { get; set; }
public string TaxNo { get; set; }
}
The class was mapped using fluent API,
Now, is there a way to update the "Status" property from the view(having Member model) without using or going to a subclass (Person/Business)?
I just tried it, but it says "Cannot create an abstract class.", when submitting the page.
Or there is a correct way to do this?
Thanks
Not in EF. You have to instantiate an object to work with EF, and you can't instantiate an abstract class.
You could make the class not be abstract. Or you could use a stored proc to update the field, or some direct sql.
It sounds like your problem is that your action method has an abstract type as a parameter, which the default model binder can't do anything with. If you are dead set on using the same view for two different classes, you may need to implement your own model binder to inspect in the incoming request and decide which type, Person or Business, to instantiate.
Check out this link for more information on creating a custom model binder:
http://odetocode.com/blogs/scott/archive/2009/05/05/iterating-on-an-asp-net-mvc-model-binder.aspx
This seems like a similar problem to the one I've answered previously here:
ASP.NET MVC 3: DefaultModelBinder with inheritance/polymorphism

JSON Nullable Deserialization Error

Suppose you have a simple struct, like so:
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
And a sample class like so:
public class Map
{
public int ID { get; set; }
public Point? PointA { get; set; }
///...
}
Now, suppose you are passing Map via AJAX as JSON. Question, what value should be passed for the not null scenario?
It may matter that JavaScriptSerializer is being used in a C# 3.5 ASP.NET ASMX web service.
The issue is what I listed in my comment about the question. The automatic properties were the issue. I converted the property and the issue was resolved.

Resources