public ActionResult Create(Artist artist) { try { if (ModelState.IsValid) { db.Artists.Add(artist); db.SaveChanges(); var dbArtist = db.Artists.Where(g => g.Name == artist.Name).SingleOrDefault(); return Json(new { Artist = dbArtist, Error = string.Empty }); } else { //TODO: better error messages string errMsg = "Something failed, probably validation"; var er = ModelState.Values.FirstOrDefault(); if (er != null && er.Value != null && !String.IsNullOrEmpty(er.Value.AttemptedValue)) errMsg = "\"" + er.Value.AttemptedValue + "\" Does not validate"; // return Json(new { Error = ModelState.Values.FirstOrDefault() }); return Json(new { Error = errMsg }); } } catch (InvalidOperationException ioex) { if (ioex.Message.Contains("Sequence contains more than one element")) return Json(new { Error = "Value provided exists in DB, enter a unique value" }); #if DEBUG return Json(new { Error = ioex.Message }); #else return Json(new { Error = "Internal Error with input provided" }); #endif } catch (Exception ex) { #if DEBUG return Json(new { Error = ex.Message }); #else return Json(new { Error = "Internal Error with input provided" }); #endif } }
/// <summary> /// Create a new Artist object. /// </summary> /// <param name="artistId">Initial value of the ArtistId property.</param> public static Artist CreateArtist(global::System.Int32 artistId) { Artist artist = new Artist(); artist.ArtistId = artistId; return artist; }
/// <summary> /// Deprecated Method for adding a new object to the Artists EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToArtists(Artist artist) { base.AddObject("Artists", artist); }