Created
March 11, 2010 16:09
-
-
Save BlackFoks/329282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// POST: /Book/Create | |
[HttpPost] | |
public ActionResult Create([Bind(Exclude = "Id")] Book bookToCreate, int[] authorIds) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(); | |
} | |
try | |
{ | |
bookToCreate.Authors.Clear(); | |
if (authorIds != null) | |
{ | |
foreach (int authorId in authorIds) | |
{ | |
Author author = _entities.Authors.Single(x => x.Id == authorId); | |
bookToCreate.Authors.Add(author); | |
} | |
} | |
else | |
{ | |
_entities.AddToBooks(bookToCreate); | |
} | |
// Нам не надо сохранять саму книгу, т.к. когда мы добавляем авторов | |
// в книгу, это происходит автоматически. | |
_entities.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
catch | |
{ | |
return View(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment