public ArtistBase ArtistMediaItemAdd(MediaItemAdd newItem) { var a = ds.Artists.Find(newItem.ArtistId); if (a == null) { return(null); } else { // Attempt to add the new item var addedItem = new MediaItem(); ds.MediaItems.Add(addedItem); addedItem.Caption = newItem.Caption; addedItem.Artist = a; // Handle the uploaded photo... // First, extract the bytes from the HttpPostedFile object byte[] photoBytes = new byte[newItem.MediaItemUpload.ContentLength]; newItem.MediaItemUpload.InputStream.Read(photoBytes, 0, newItem.MediaItemUpload.ContentLength); // Then, configure the new object's properties addedItem.Content = photoBytes; addedItem.ContentType = newItem.MediaItemUpload.ContentType; ds.SaveChanges(); return((addedItem == null) ? null : Mapper.Map <ArtistBase>(a)); } }
public ActionResult AddMediaItem(int?id, MediaItemAdd newItem) { // Validate the input // Two conditions must be checked if (!ModelState.IsValid && id.GetValueOrDefault() == newItem.ArtistId) { return(View(newItem)); } // Process the input var addedItem = m.ArtistMediaItemAdd(newItem); if (addedItem == null) { return(View(newItem)); } else { return(RedirectToAction("DetailsWithMediaItem", new { id = addedItem.Id })); } }