Exemple #1
0
        public async Task <ActionResult> Genre(MusicViewModel genre)
        {
            var model = new MusicViewModel()
            {
                Genres = await _genreRepository.GetAllyAsync()
            };
            var genreModel = new Genre();

            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError(string.Empty, "لطفا مقدار های مناسب پر کنید");
                }
                if (string.IsNullOrEmpty(genre.GenreOldName))
                {
                    genre.GenreOldName = genre.GenreNewName;
                }

                genreModel = await _genreRepository.GetByNameAsync(genre.GenreOldName);

                if (genre.ActionType == "create" || genre.ActionType == "edit")
                {
                    if (genreModel == null)
                    {
                        await _genreRepository.CreateAsync(new Genre
                        {
                            Name = genre.GenreOldName,
                        });

                        model.Genres = await _genreRepository.GetAllyAsync();

                        //return RedirectToAction("Section", new { surveyName = surveys.SurveyTitle });
                        return(View(model));
                    }
                    else
                    {
                        genreModel.Name = (genre.GenreOldName == genre.GenreNewName ? genre.GenreOldName : genre.GenreNewName);

                        await _genreRepository.EditAsync(genreModel.Id, genreModel);
                    }
                }
                else
                {
                    await _genreRepository.DeleteAsync(genreModel.Id);
                }

                model.Genres = await _genreRepository.GetAllyAsync();

                //return RedirectToAction("Section", new { surveyName = model.GenreOldName });
                return(View(model));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
                return(View(model: model));
            }
        }