8

I've been stuck with this for a while and I can't seem to figure it out. Appreciate any help!

This is my model: http://www.jsoneditoronline.org/?id=9ee3466c40627f33c284e63544c8b8a7

I have the proper C# objects set up like this:

public class Media
{
    public string name { get; set; }
    public string title { get; set; }
    public string album { get; set; }
    public string artist { get; set; }
    public string length { get; set; }
    public int bitrate { get; set; }
    public double size { get; set; }
    public string start_time { get; set; }
    public string mimetype { get; set; }
    public string hash { get; set; }
}

public class Playlist
{
    public string name { get; set; }
    public List<Media> media { get; set; }
    public List<Graphics> graphics { get; set; }
    public bool shuffle { get; set; }
    public int volume { get; set; }
    public string start_time { get; set; }
    public string end_time { get; set; }
}

public class Day
{
    public string name { get; set; }
    public List<Playlist> playlists { get; set; }
}


public class Schedule
{
    public List<Day> days { get; set; }
    public string hash { get; set; }
}

I need to POST this whole JSON object directly from the MVC Controller. On other occasions I'd like to PUT the schedule. How can I properly handle this? Examples could really help.

Thanks!

I'm already doing the below for POST:

var schedule = JsonConvert.DeserializeObject<Schedule>(model.ToString());

This is working as expected however, sometimes related Media objects already exist in the database and it is causing an Internal Server Error when trying to INSERT the same Media object (which already exists) - The [Key] for Media is the hash property.

user1027620
  • 2,745
  • 5
  • 37
  • 65
  • 2
    As far as I can understand your actual problem does not have anything to do with MVC or JSON. You need some help to avoid primary key violation errors. Is that correct? – Kosala W Dec 12 '15 at 04:14
  • @KosalaW Sort of yeah, Changing the Primary key to an `int id` resolves this. But recreated the same existing `Media` object in the database again. I don't want duplicates when I already have the object... – user1027620 Dec 12 '15 at 04:18
  • 2
    So before you insert `Media` in to your table, you need to check if that `Media` exists in the table already. If it does, update it, else insert. – Kosala W Dec 12 '15 at 04:21
  • `Media` is inserted automatically as soon as I do this -> `db.Schedule.add(schedule)` since `Media` is a nested object in there. – user1027620 Dec 12 '15 at 04:23
  • So before you call `db.Schedule.add(schedule)`, do you check if `schedule` exists in the database?. Can you show the structure of your entity classes? The classes that you have published seem to be viewmodels. I can't see any PKs in them. – Kosala W Dec 12 '15 at 04:28
  • @KosalaW Schedule can be new to the db. But the nested related media files can exist. – user1027620 Dec 12 '15 at 15:58
  • It is not possible to provide an answer without seeing your entity structure. Do you have a `MediaId` field in `Schedule` entity? – Kosala W Dec 12 '15 at 22:46
  • Media have primaryKey?, because instead to add, you can attach the entity can change the state to Added, Unchanged or Modified of the Media – Byron Dec 17 '15 at 20:48
  • @Byron yeah, the pk is the "hash" property – user1027620 Dec 18 '15 at 03:21

2 Answers2

4

You need to serialize Day class.

using Newtonsoft.json nuget package you need to serialize it as object. It will automatically serialize complex object to json

List<Day> days = // list of days result
var jsonData= JsonConvert.SerializeObject(days);
return json(jsonData);

Update

As per your update serialize and deserialize functions are working properly. You are facing issue while inserting records in Media. Hash is not unique. and Hash Collision is possible. You need to improve hash generation code to use hash as identical. Useful links to understand hash

Community
  • 1
  • 1
Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28
1

You can use the extension method AddOrUpdate

using System.Data.Entity.Migrations;

db.Schedule.AddOrUpdate(schedule)
Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40