// The id parameter name should match the DataKeyNames value set on the control public void GridViewPlaylists_UpdateItem(int id) { YouTubePlaylists.Models.Playlist item = null; // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); } }
public void FormViewPlaylist_InsertItem() { var playlist = new YouTubePlaylists.Models.Playlist(); TryUpdateModel(playlist); playlist.CreatorId = Context.User.Identity.GetUserId(); var tbVideoUrls = (TextBox)FormViewPlaylist.FindControl("tbVideoUrls"); if (tbVideoUrls != null) { var videoUrls = tbVideoUrls.Text.Split(' '); foreach (var url in videoUrls) { var video = new Video { Url = url }; playlist.Videos.Add(video); } } try { data.Playlists.Add(playlist); data.SaveChanges(); master.ShowSuccessMessage("Playlist added."); Response.Redirect("/Playlists/Playlist.aspx?id=" + playlist.Id); } catch (Exception e) { master.ShowErrorMessage("Playlist not added. Invalid data."); } }