MVC 4 Beta - you must write the attribute 'type' = 'object' after writing the attribute with the local name '__type'

I am working with WebAPI in the new beta version of MVC 4. I came across this error while trying to get an object that has a virtual ICollection<> property to populate. Is there any way around this now? I understand that this is at the Beta stage, so it can be fixed along the way. It was just curiosity if there was a solution for this.

+6
source share
4 answers

I got this to work by deleting the virtual keyword and making sure that the objects and collections that had the virtual keyword were provided in the Include statement in my repository.

 public class Order { public int ID { get; set; } public DateTime OrderDate { get; set; } public ICollection<Product> Products { get; set; } } public interface IOrderRepository { IQueryable<Order> Orders { get; } void SaveOrder(Order order); void DeleteOrder(Order order); } public class OrderRepository { StoreDbContext db = new StoreDbContext(); public IQueryable<Order> Orders { get { return db.Orders.Include("Products"); } } public void SaveOrder(Order order) { db.Entry(order).State = order.ID == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); } public void DeleteOrder(Order order) { db.Orders.Remove(order); db.SaveChanges(); } } 
+2
source

I had the same problem, it seems to be the problem with the default WebApi serializer. I added Json.Net as formatting to my Global.asax.cs file, and it worked fine for me. I just followed this example: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx

This is what I got in my Global.asax.cs file

 JsonSerializerSettings serializerSettings = new JsonSerializerSettings(); serializerSettings.Converters.Add(new IsoDateTimeConverter()); GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings); 

I just added the Json.Net package using NuGet and created the JsonNetFormatter class as described in the post above.

+1
source

I had a similar problem. I fixed it using the ViewModel class, which had only simple types. I translated the object returned by DbContext into my ViewModel class and passed it back to the client.

It would not work in all situations, but it was in mine.

0
source

This problem also enters. My situation was a little different though.

I had this structure and it would not work for me.

 [DataContract] public class MJPEGCamera : Camera { } [DataContract] public class H264Camera : Camera { } [DataContract] public class Camera { [DataMember] public string cameraName { get; set; } [DataMember] public string address { get; set; } [DataMember] public string format { get; set; } [DataMember] public string archiveDaysUrl { get; private set; } [DataMember] public string archiveHoursUrl { get; private set; } } 

So, I just created a factory in the camera to accomplish what I need. Hope this helps someone who finds this answer.

0
source

Source: https://habr.com/ru/post/910549/


All Articles