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.
int idresolves this. But recreated the same existingMediaobject in the database again. I don't want duplicates when I already have the object...Mediain to your table, you need to check if thatMediaexists in the table already. If it does, update it, else insert.Mediais inserted automatically as soon as I do this ->db.Schedule.add(schedule)sinceMediais a nested object in there.db.Schedule.add(schedule), do you check ifscheduleexists 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.