public bool Create(StoryCreate model) { using (var ctx = new ApplicationDbContext()) { MultiSelectList charList = new MultiSelectList(ctx.Characters.ToList().OrderBy(i => i.CharName), "ID", "CharName"); model = new StoryCreate { Characters = charList }; var story = new Story { StoryName = model.StoryName, Description = model.Description, }; ctx.Stories.Add(story); return(ctx.SaveChanges() == 1); } //foreach (int i in model.Characters) //{ // story.Characters = ctx.Characters.Where(o => o.ID == i).ToList(); //} //var allCharacterList = ctx.Characters.Include(t => t.Stories).ToList(); //story.Characters = (List<Character>)allCharacterList.Where(o => o.ID == model); //var allCharacterList = ctx.Characters.ToList(); //entity.Characters = allCharacterList.Where(o => o.ID == id); //using (var ctx = new ApplicationDbContext()) //{ // foreach(Character character in storyCreate.Characters) // { // entity.Characters.Add(character); // } // ctx.Stories.Add(entity); //using (var ctx = new ApplicationDbContext()) //{ // var story = ctx.Stories // .Include(p => p.Characters) // .Single(p => p.ID == entity.ID); // var newCharacter = ctx.Characters.Find(entity.ID); // story.Characters.Add(new Character // { // Stories = story, // Character = newCharacter, // }); //} //return ctx.SaveChanges() == 1; }
public IHttpActionResult Post(StoryCreate model) { var service = CreateStoryService(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!service.CreateStory(model)) { return(InternalServerError()); } return(Ok($"The story '{model.Title}' has been published.")); }
public bool CreateStory(StoryCreate model) { var storyEntity = new Story { CategoryId = (_context.Categories.Single(c => c.Name.ToLower() == model.CategoryName.ToLower())).Id, WriterId = (_context.Writers.Single(w => w.Name.ToLower() == model.WriterName.ToLower())).Id, Title = model.Title, Body = model.Body, Location = model.Location, TimeOfPublication = DateTimeOffset.Now, AuthorId = _authorId }; _context.Stories.Add(storyEntity); return(_context.SaveChanges() == 1); }
public ActionResult Create() { //var charServices = new CharService(); //var charList = charServices.GetCharactersCharacters(); //ViewBag.Characters = new SelectList(charList, "ID", "CharName"); //var character = new CharListItem(); //character.Characters = new List<CharListItem>(); //return View(new StoryCreate()); MultiSelectList charList = new MultiSelectList(db.Characters.ToList().OrderBy(p => p.CharName), "ID", "CharName"); StoryCreate story = new StoryCreate { Characters = charList }; return(View(story)); }
private void Awake() { storyCreate = target as StoryCreate; }
public ActionResult Create([Bind(Include = "StoryName, Description, CharacterIds")] StoryCreate model) { if (ModelState.IsValid) { Story story = new Story { StoryName = model.StoryName, ID = model.ID, Description = model.Description }; if (model.CharacterIds != null) { foreach (var id in model.CharacterIds) { var charId = int.Parse(id); var character = db.Characters.Find(charId); try { story.Characters.Add(character); } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Story", "Index"))); } } } try { db.Stories.Add(story); db.SaveChanges(); } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Story", "Index"))); } return(RedirectToAction("Details", new { id = story.ID })); } else { ModelState.AddModelError("", "Something failed."); return(View(model)); } // if (!ModelState.IsValid) // { // //var charServices = new CharService(); // //var charList = charServices.GetCharactersCharacters(); // //ViewBag.CharactersForStory = new SelectList(charList, "ID", "CharName"); // //return View(model); // MultiSelectList charList = new MultiSelectList(db.Teams.ToList().OrderBy(i => i.Name), "TeamId", "Name"); // } // var service = new StoryService(); // if (service.Create(model)) // { // var charServices = new CharService(); // var charList = charServices.GetCharactersCharacters(); // ViewBag.CharactersForStory = new SelectList(charList, "ID", "CharName"); // return RedirectToAction("Index"); // } // ModelState.AddModelError("", "Story could not be added"); // return View(model); ; }