Exemplo n.º 1
0
        public ActionResult Create([Bind(Prefix = "Utente")] Utente utente)
        {
            if (utente.IdRuolo == 2 && utente.IdFornitore.HasValue == false)
            {
                ModelState.AddModelError("IdFornitore", "Per questo ruolo, il campo fornitore è obbligatorio");
            }
            if (!String.IsNullOrEmpty(utente.Email))
            {
                var utenteEsistente = Context.Utenti
                                      .Where(w => w.Email == utente.Email).FirstOrDefault();
                if (utenteEsistente != null)
                {
                    ModelState.AddModelError("Email", "La email da te selezionata è già esistente");
                }
            }
            //Validazione dell'input
            if (ModelState.IsValid)
            {
                //Inserimento all'interno del database
                Context.Utenti.Add(utente);
                Context.SaveChanges();

                //Faccio la redirect alla lista degli utenti
                return(RedirectToAction("Index"));
            }

            UtentiCreateViewModel vm = new UtentiCreateViewModel();

            vm.Utente    = utente;
            vm.Ruoli     = Context.Ruoli.OrderBy(o => o.DescrizioneRuolo).ToList();
            vm.Fornitori = Context.Fornitori.OrderBy(o => o.RagioneSociale).ToList();


            return(View(vm));
        }
Exemplo n.º 2
0
        public ActionResult Create()
        {
            Utente u = new Utente();
            UtentiCreateViewModel vm = new UtentiCreateViewModel();

            vm.Utente    = u;
            vm.Ruoli     = Context.Ruoli.OrderBy(o => o.DescrizioneRuolo).ToList();
            vm.Fornitori = Context.Fornitori.OrderBy(o => o.RagioneSociale).ToList();
            return(View(vm));
        }