//
 // GET: /Textbook/Details/5
 public ActionResult Details(int id)
 {
     using (InstManager)
     {
         using (PublishersManager)
         {
             using (AuthorsManager)
             {
                 using (PeopleManager)
                 {
                     var tb   = TBManager.GetTextbookbyID(id);
                     var disp = Mapper.Map <vmTextbook>(tb);
                     if (disp != null)
                     {
                         disp.Publisher     = Mapper.Map <vmPublisher>(PublishersManager.GetPublisherbyID(tb.PublisherID));
                         disp.Author        = Mapper.Map <vmAuthor>(AuthorsManager.GetAuthorbyID(tb.AuthorID));
                         disp.Author.Person = Mapper.Map <vmPerson>(PeopleManager.GetPersonbyID(disp.Author.PersonID));
                     }
                     else
                     {
                         disp = new vmTextbook();
                         ModelState.AddModelError("", "Failed to load details for requested item.");
                     }
                     return(View(disp));
                 }
             }
         }
     }
 }
        private void PublisherDataBind()
        {
            DataSet ds = new PublishersManager().GetAllList();
            DataRow dr = ds.Tables[0].NewRow();

            dr["Id"]   = 0;
            dr["Name"] = "--请选择--";
            ds.Tables[0].Rows.InsertAt(dr, 0);
            ddlPublisher.DataSource = ds;
            ddlPublisher.DataBind();
        }
 public ActionResult Create(vmTextbook tb)
 {
     try
     {
         using (InstManager)
         {
             using (PublishersManager)
             {
                 using (AuthorsManager)
                 {
                     using (PeopleManager)
                     {
                         var person  = Mapper.Map <Person>(tb.Author.Person);
                         var success = PeopleManager.AddPerson(person);
                         if (success)
                         {
                             var author = Mapper.Map <Author>(tb.Author);
                             author.PersonID = person.ID;
                             success         = AuthorsManager.AddAuthor(author);
                             if (success)
                             {
                                 var pub = Mapper.Map <Publisher>(tb.Publisher);
                                 success = PublishersManager.AddPublisher(pub);
                                 if (success)
                                 {
                                     var text = Mapper.Map <Textbook>(tb);
                                     text.AuthorID    = author.ID;
                                     text.PublisherID = pub.ID;
                                     success          = TBManager.AddTextbook(text);
                                     if (success)
                                     {
                                         RedirectToAction("Details", new { id = text.ID });
                                     }
                                     else
                                     {
                                         throw new DataException("Failed to save textbook. Please try again.");
                                     }
                                 }
                                 else
                                 {
                                     throw new DataException("Failed to save publisher. Please try again.");
                                 }
                             }
                             else
                             {
                                 throw new DataException("Failed to save author. Please try again.");
                             }
                         }
                         else
                         {
                             throw new DataException("Failed to save person. Please try again.");
                         }
                     }
                 }
             }
         }
     }
     catch (DataException ex)
     {
         //Log the error (add a variable name after DataException)
         ModelState.AddModelError("", ex.Message);
     }
     return(View(tb));
 }
 public ActionResult Edit(vmTextbook tb)
 {
     try
     {
         using (InstManager)
         {
             using (PublishersManager)
             {
                 using (AuthorsManager)
                 {
                     using (PeopleManager)
                     {
                         var text = TBManager.GetTextbookbyID(tb.ID);
                         text.Name        = tb.Name;
                         text.PublishDate = tb.PublishDate;
                         var success = TBManager.UpdateTextbook(text);
                         if (success)
                         {
                             var auth = AuthorsManager.GetAuthorbyID(text.AuthorID);
                             var pers = PeopleManager.GetPersonbyID(auth.PersonID);
                             pers.FirstMidName = tb.Author.Person.FirstMidName;
                             pers.LastName     = tb.Author.Person.LastName;
                             success           = PeopleManager.UpdatePerson(pers);
                             if (success)
                             {
                                 var pub = PublishersManager.GetPublisherbyID(text.PublisherID);
                                 pub.Name  = tb.Publisher.Name;
                                 pub.City  = tb.Publisher.City;
                                 pub.State = tb.Publisher.State;
                                 success   = PublishersManager.UpdatePublisher(pub);
                                 if (success)
                                 {
                                     return(RedirectToAction("Details", new { id = tb.ID }));
                                 }
                                 else
                                 {
                                     throw new DataException("Failed to save publisher. Please try again.");
                                 }
                             }
                             else
                             {
                                 throw new DataException("Failed to save author. Please try again.");
                             }
                         }
                         else
                         {
                             throw new DataException("Failed to save textbook. Please try again.");
                         }
                     }
                 }
             }
         }
     }
     catch (DataException ex)
     {
         //Log the error (add a variable name after DataException)
         ModelState.AddModelError("", ex.Message);
     }
     tb.Publisher     = Mapper.Map <vmPublisher>(PublishersManager.GetPublisherbyID(tb.PublisherID));
     tb.Author        = Mapper.Map <vmAuthor>(AuthorsManager.GetAuthorbyID(tb.AuthorID));
     tb.Author.Person = Mapper.Map <vmPerson>(PeopleManager.GetPersonbyID(tb.Author.PersonID));
     return(View(tb));
 }