示例#1
0
        public ActionResult Create([Bind("Name,Email,Password,RG,cpf,Street,Neighborhood,idCity,idState,idUser")] VMInstructor payload)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    TempData["_mensagem"] = new VMMessages()
                    {
                        Css  = "alert alert-danger",
                        Text = "Não foi possivel realizar seu cadastro,tente novamente!"
                    };
                    ViewData["idCity"] = new SelectList(_cityService.GetCitiesByState(payload.idState), "Id", "Name", payload.idCity);

                    ViewData["idState"] = new SelectList(_stateService.GetAll(), "Id", "Name");
                    return(View(payload));
                }
                var result = _instructorService.Save(payload);
                var _msg   = new VMMessages()
                {
                    Css  = "alert alert-success",
                    Text = "Cadastro salvo com sucesso!"
                };
                TempData["_mensagem"] = JsonConvert.SerializeObject(_msg);
                return(RedirectToAction(nameof(Index)));
            }
            catch (CustomHttpException ex)
            {
                _logger.Log(LogLevel.Error, ex.Message);
                TempData["_mensagem"] = new VMMessages()
                {
                    Css  = "alert alert-danger",
                    Text = ex.ErrorMessage
                };
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, ex.Message);
                TempData["_mensagem"] = new VMMessages()
                {
                    Css  = "alert alert-danger",
                    Text = "Um erro insperado ocorreu"
                };
            }
            ViewData["idCity"] = new SelectList(_cityService.GetCitiesByState(payload.idState), "Id", "Name", payload.idCity);

            ViewData["idState"] = new SelectList(_stateService.GetAll(), "Id", "Name");
            return(View(payload));
        }