Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (books_categories bc in book.books_categories)
- {
- category_names.Add(bc.Category.category.Trim());
- }
- public partial class Books
- {
- public IEnumerable<string> CategoryNames
- {
- get
- {
- return this.Books_Categories
- .Select( bc => bc.Category.category.Trim() );
- }
- }
- public void AddCategory( Category category )
- {
- this.Books_Categories.Add( new Book_Category
- {
- Category = category,
- Book = this
- } );
- }
- public void RemoveCategory( Category category )
- {
- var bc = this.Book_Categories
- .Where( c => c.Category == category )
- .SingleOrDefault();
- this.Books_Categories.Remove( bc );
- }
- }
- <p>
- <label for="CategorySelect">Category:</label>
- <%= Html.ListBox("CategorySelect") %>
- <%= Html.ValidationMessage("CategorySelect", "*")%>
- </p>
- public ActionResult New()
- {
- var data = _entities.Categories.ToList();
- ViewData["CategorySelect"] = new MultiSelectList(data, "Id", "Name");
- }
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult New([Bind(Exclude = "Id")] Book Booknew)
- {
- IEnumerable<int> selectedCategories = Request["CategorySelect"].Split(new Char[] { ',' }).Select(idStr => int.Parse(idStr));
- if (!ModelState.IsValid)
- return View();
- try {
- foreach(var item in selectedCategories){
- BooksCategories bc = new BooksCategories();
- bc.Book = Booknew;
- bc.CategoryId = item;
- _entities.BooksCategories.InsertOnSubmit(bc);
- }
- _entities.Books.InsertOnSubmit(Booknew);
- _entities.SubmitChanges();
Add Comment
Please, Sign In to add comment