Skip to content

Instantly share code, notes, and snippets.

@BlackFoks
Created March 11, 2010 16:09
Show Gist options
  • Save BlackFoks/329282 to your computer and use it in GitHub Desktop.
Save BlackFoks/329282 to your computer and use it in GitHub Desktop.
//
// 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