Ejemplo n.º 1
0
        public async Task <IActionResult> Insert(BooksCreatViewModel ViewModel)
        {
            if (ModelState.IsValid)
            {
                List <Book_Translator> translators = new List <Book_Translator>();
                List <Book_Category>   categories  = new List <Book_Category>();
                if (ViewModel.TranslatorID != null)
                {
                    translators = ViewModel.TranslatorID.Select(a => new Book_Translator {
                        TranslatorID = a
                    }).ToList();
                }

                DateTime?InsertDate = null;
                InsertDate = DateTime.Now;

                Book book = new Book()
                {
                    Delete       = false,
                    ISBN         = ViewModel.ISBN,
                    IsLoan       = ViewModel.IsLoan,
                    NumOfPages   = ViewModel.NumOfPages,
                    LanguageID   = ViewModel.LanguageID,
                    Summary      = ViewModel.Summary,
                    Title        = ViewModel.Title,
                    InsertDate   = InsertDate,
                    author_Books = ViewModel.AuthorID.Select(a => new Author_Book {
                        AuthorID = a
                    }).ToList(),
                    book_Tranlators = translators,
                };
                if (ViewModel.Image != null)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await ViewModel.Image.CopyToAsync(memoryStream);

                        book.Image = memoryStream.ToArray();
                    }
                }
                await _UW.BaseRepository <Book>().CreateAsync(book);

                await _UW.Commit();

                return(RedirectToAction("Index"));
            }
            else
            {
                ViewBag.LanguageID = new SelectList(_UW.BaseRepository <Language>().FindAll(), "LanguageID", "LanguageName");
                ViewBag.AuthorID   = new SelectList(_UW.BaseRepository <Author>().FindAll().Select(t => new AuthorList {
                    AuthorID = t.AuthorID, NameFamily = t.FirstName + " " + t.LastName
                }), "AuthorID", "NameFamily");
                ViewBag.TranslatorID = new SelectList(_UW.BaseRepository <Translator>().FindAll().Select(t => new TranslatorList {
                    TranslatorID = t.TranslatorID, NameFamily = t.Name + " " + t.Family
                }), "TranslatorID", "NameFamily");
                return(View(ViewModel));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Insert()
        {
            ViewBag.LanguageID = new SelectList(_UW.BaseRepository <Language>().FindAll(), "LanguageID", "LanguageName");
            ViewBag.AuthorID   = new SelectList(_UW.BaseRepository <Author>().FindAll().Select(t => new AuthorList {
                AuthorID = t.AuthorID, NameFamily = t.FirstName + " " + t.LastName
            }), "AuthorID", "NameFamily");
            ViewBag.TranslatorID = new SelectList(_UW.BaseRepository <Translator>().FindAll().Select(t => new TranslatorList {
                TranslatorID = t.TranslatorID, NameFamily = t.Name + " " + t.Family
            }), "TranslatorID", "NameFamily");

            BooksCreatViewModel ViewModel = new BooksCreatViewModel();

            return(View(ViewModel));
        }