@@ -149,6 +149,9 @@ public ActionResult Create()
149149 {
150150 PopulateAuthorDropDownList ( ) ;
151151 PopulateCategoryDropDownList ( ) ;
152+ PopulatePublishersDropDownList ( ) ;
153+ PopulateMainSubjectDropDownList ( ) ;
154+ PopulateSubjectDropDownList ( ) ;
152155 return View ( ) ;
153156 }
154157
@@ -157,9 +160,8 @@ public ActionResult Create()
157160 // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
158161 [ HttpPost ]
159162 [ ValidateAntiForgeryToken ]
160- public ActionResult Create ( [ Bind ( Include = "BookID,Title,AlternativeTitle,AuthorID,CategoryID,InitialPrintedYear,ActualPrintYear,Language,Material,Read,Pages,ISBN,Website,CoverLink" ) ] Book book )
163+ public ActionResult Create ( [ Bind ( Include = "BookID,Title,AlternativeTitle,AuthorID,CategoryID,InitialPrintedYear,ActualPrintYear,Language,Material,Read,Pages,ISBN,Website,CoverLink,Rating,CodeWithinSerie,Condition,ReviewNote,Location " ) ] Book book )
161164 {
162- book . CreationDate = DateTime . Now ;
163165 if ( ModelState . IsValid )
164166 {
165167
@@ -169,9 +171,21 @@ public ActionResult Create([Bind(Include = "BookID,Title,AlternativeTitle,Author
169171 }
170172 PopulateAuthorDropDownList ( book . AuthorID ) ;
171173 PopulateCategoryDropDownList ( book . CategoryID ) ;
174+ PopulatePublishersDropDownList ( book . PublisherID ) ;
175+ PopulateMainSubjectDropDownList ( book . MainSubjectID ) ;
176+
177+ if ( book . Subjects . Count > 0 )
178+ {
179+ PopulateSubjectDropDownList ( book . Subjects . First ( ) . SubjectID ) ;
180+ } else
181+ {
182+ PopulateSubjectDropDownList ( ) ;
183+ }
184+
172185 return View ( book ) ;
173186 }
174187
188+ #region dropdowns
175189 private void PopulateAuthorDropDownList ( object selected = null )
176190 {
177191 var query = _db . Query < Author > ( ) . OrderBy ( a => a . Lastname ) ;
@@ -182,6 +196,27 @@ private void PopulateCategoryDropDownList(object selected = null)
182196 var query = _db . Query < Category > ( ) . OrderBy ( a => a . Title ) ;
183197 ViewBag . CategoryID = new SelectList ( query , "CategoryID" , "Title" , selected ) ;
184198 }
199+
200+ private void PopulatePublishersDropDownList ( object selected = null )
201+ {
202+ var query = _db . Query < Publisher > ( ) . OrderBy ( a => a . Name ) ;
203+ ViewBag . PublisherID = new SelectList ( query , "PublisherID" , "Name" , selected ) ;
204+ }
205+
206+ private void PopulateMainSubjectDropDownList ( object selected = null )
207+ {
208+ var query = _db . Query < Subject > ( ) . OrderBy ( a => a . Name ) ;
209+ ViewBag . MainSubjectID = new SelectList ( query , "MainSubjectID" , "Name" , selected ) ;
210+ }
211+
212+ private void PopulateSubjectDropDownList ( object selected = null )
213+ {
214+ var query = _db . Query < Subject > ( ) . OrderBy ( a => a . Name ) ;
215+ ViewBag . SubjectID = new SelectList ( query , "SubjectID" , "Name" , selected ) ;
216+ }
217+ #endregion
218+
219+
185220 // GET: Books/Edit/5
186221 public ActionResult Edit ( int ? id )
187222 {
@@ -194,6 +229,9 @@ public ActionResult Edit(int? id)
194229 {
195230 return HttpNotFound ( ) ;
196231 }
232+ PopulateAuthorDropDownList ( book . AuthorID ) ;
233+ PopulateCategoryDropDownList ( book . CategoryID ) ;
234+ PopulatePublishersDropDownList ( book . PublisherID ) ;
197235 return View ( book ) ;
198236 }
199237
@@ -202,14 +240,17 @@ public ActionResult Edit(int? id)
202240 // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
203241 [ HttpPost ]
204242 [ ValidateAntiForgeryToken ]
205- public ActionResult Edit ( [ Bind ( Include = "BookID,Title,AlternativeTitle,CreationDate,InitialPrintedYear,ActualPrintYear,Language,Material,Read,Pages,ISBN,Website,CoverLink" ) ] Book book )
243+ public ActionResult Edit ( [ Bind ( Include = "BookID,Title,AlternativeTitle,CreationDate,InitialPrintedYear,ActualPrintYear,Language,Material,Read,Pages,ISBN,Website,CoverLink,Rating,CodeWithinSerie,Condition,ReviewNote,Location " ) ] Book book )
206244 {
207245 if ( ModelState . IsValid )
208246 {
209247 _db . Update ( book ) ;
210248 _db . SaveChanges ( ) ;
211249 return RedirectToAction ( "Index" ) ;
212250 }
251+ PopulateAuthorDropDownList ( book . AuthorID ) ;
252+ PopulateCategoryDropDownList ( book . CategoryID ) ;
253+ PopulatePublishersDropDownList ( book . PublisherID ) ;
213254 return View ( book ) ;
214255 }
215256
0 commit comments