Ejemplo n.º 1
0
        public ActionResult Register([Bind(Include = "Cedula,TelefonoMovil,FullName,ProvinciaId,MunicipioId,Sector,Email,IsInstagram,IsFacebook,IsTwitter,IsOther,ColaboradorDigitalRedes,Comentario,CoordinadorGuid,CascadingModel,Instagram,CodigoColegioElectoral,DistritoMunicipal,CoordinadorId")] ContactForm model)
        {
            model.CascadingModel.Provincias = _context.Provincias.Select(x => new SelectListItem {
                Text = x.Name, Value = x.Id.ToString()
            }).ToList();

            ViewBag.IsSuccess = false;
            model.ConcatRedes();
            //var sd = Guid.Parse(Session["UserFormId"].ToString());
            //model.CoordinadorGuid = (model.CoordinadorGuid == Guid.Empty || model.CoordinadorGuid == null ? Guid.Parse(Session["UserFormId"].ToString()) : model.CoordinadorGuid);

            //ViewBag.UserFormId = model.CoordinadorGuid;
            var coordinador = _context.ContactForms.FirstOrDefault(x => x.Cedula == (model.Cedula.Replace("-", "").Trim()));

            if (coordinador != null)
            {
                ViewBag.Message            = "Ya existe un elector con este numero de cedula!";
                model.CascadingModel.Users = _context.Users.Where(x => x.Activo == true && x.UserName != "admin").Select(x => new SelectListItem {
                    Text = x.Nombres + " " + x.Apellidos, Value = x.Id.ToString()
                }).ToList();
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                ViewBag.Message = String.Join(",", ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage));

                return(View(model));
            }

            //model.CoordinadorId = coordinador.Id;
            model.Cedula = model.Cedula.Replace("-", "").Trim();

            _context.ContactForms.Add(model);
            _context.SaveChanges();
            ViewBag.IsSuccess = true;
            ViewBag.Message   = "Registrado correctamente!";
            //model = new ContactForm();
            ModelState.Clear();

            return(RedirectToAction("Success"));
        }