Example #1
0
        public ActionResult Create(AlbumPO album)
        {
            //Defaults redirect to index of album controller.
            ActionResult oResponse = RedirectToAction("Index", "Album");

            if (ModelState.IsValid)
            {
                try
                {
                    //Adds album to datatable using valid album.
                    dataAccess.CreateAlbum(AlbumMapper.MapPoToDO(album));
                    TempData["Message"] = "Album successfully created.";
                }
                catch (Exception e)
                {
                    //Logs exception using exceptionLog class.
                    exceptionLog.ExceptionLog("Critical", e.Message, "AlbumController", "Create", e.StackTrace);


                    try
                    {
                        List <UserDO> dataObjects = userData.ReadUsers();
                        ViewBag.DropDown = new List <SelectListItem>();
                        foreach (UserDO user in dataObjects)
                        {
                            //Adds username and user Id to a dropdown list of users in viewbag.
                            ViewBag.DropDown.Add(new SelectListItem()
                            {
                                Text = user.Username, Value = user.UserId.ToString()
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        //Logs exception using exceptionLog class.
                        exceptionLog.ExceptionLog("Critical", ex.Message, "AlbumController", "Create", ex.StackTrace);
                    }

                    oResponse = View(album);
                }
            }
            else
            {
                //Modelstate was invalid.
                try
                {
                    List <UserDO> dataObjects = userData.ReadUsers();
                    ViewBag.DropDown = new List <SelectListItem>();
                    foreach (UserDO user in dataObjects)
                    {
                        //Adds username and user Id to a dropdown list of users in viewbag.
                        ViewBag.DropDown.Add(new SelectListItem()
                        {
                            Text = user.Username, Value = user.UserId.ToString()
                        });
                    }
                }
                catch (Exception ex)
                {
                    //Logs exception using exceptionLog class.
                    exceptionLog.ExceptionLog("Critical", ex.Message, "AlbumController", "Create", ex.StackTrace);
                }
                oResponse = View(album);
            }
            return(oResponse);
        }