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.

9
  • 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? Commented Dec 12, 2015 at 4: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... Commented Dec 12, 2015 at 4: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. Commented Dec 12, 2015 at 4:21
  • Media is inserted automatically as soon as I do this -> db.Schedule.add(schedule) since Media is a nested object in there. Commented Dec 12, 2015 at 4: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. Commented Dec 12, 2015 at 4:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

The parent object is of type Schedule - Please check my edit
Please check my updated answer. Hit as an answer if you found it useful.
1

You can use the extension method AddOrUpdate

using System.Data.Entity.Migrations;

db.Schedule.AddOrUpdate(schedule)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.