Example #1
0
        public ActionResult Delete([Bind(Prefix = "Fornitore")] Fornitore fornitore, bool ultimoFornitore, int paginaCorrente = 1)
        {
            bool success = false;

            try
            {
                if (fornitore != null)
                {
                    if (fornitore.Documenti.Count() == 0)
                    {
                        Context.Entry(fornitore).State = System.Data.Entity.EntityState.Deleted;
                        //Context.Utenti.Remove(utente);
                        Context.SaveChanges();
                        success = true;
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Impossibile eliminare il fornitore");
                    }
                }
                else
                {
                    ModelState.AddModelError("Error", "Fornitore sconosciuto");
                }
            }
            catch (Exception ex)
            {
                success = false;
                ModelState.AddModelError("Error", ex.Message);
            }
            if (success)
            {
                FornitoriIndexViewModel vm = FornitoriIndexViewModel.Load(Context, ultimoFornitore && paginaCorrente > 1 ? paginaCorrente - 1 : paginaCorrente, PageSize);
                return(Json(new { CloseDialog = true,
                                  Html = RenderRazorViewToString("_List", vm),
                                  TargetId = "table_container",
                                  Success = true }, JsonRequestBehavior.DenyGet));
            }

            FornitoriDeleteViewModel vmErrore = new FornitoriDeleteViewModel();

            vmErrore.Fornitore = fornitore;


            return(Json(new
            {
                CloseDialog = false,
                Html = RenderRazorViewToString("_Delete", vmErrore),
                TargetId = "modalContent",
                Success = false
            }, JsonRequestBehavior.DenyGet));
        }
Example #2
0
        public ActionResult Index()
        {
            FornitoriIndexViewModel vm = FornitoriIndexViewModel.Load(Context, 1, PageSize);

            return(View(vm));
        }
Example #3
0
        public ActionResult List(int paginaCorrente)
        {
            FornitoriIndexViewModel vm = FornitoriIndexViewModel.Load(Context, paginaCorrente, PageSize);

            return(PartialView("_List", vm));
        }
Example #4
0
        public ActionResult Edit([Bind(Prefix = "Fornitore")] Fornitore fornitore, int paginaCorrente = 1)
        {
            bool success = false;

            if (String.IsNullOrEmpty(fornitore.RagioneSociale))
            {
                ModelState.AddModelError("Fornitore.RagioneSociale", "La ragione sociale è obbligatoria");
            }
            if (String.IsNullOrEmpty(fornitore.Citta))
            {
                ModelState.AddModelError("Fornitore.Citta", "La città è obbligatoria");
            }
            if (String.IsNullOrEmpty(fornitore.Indirizzo))
            {
                ModelState.AddModelError("Fornitore.Indirizzo", "L'indirizzo è obbligatorio");
            }
            if (String.IsNullOrEmpty(fornitore.Telefono))
            {
                fornitore.Telefono = String.Empty;
            }
            //Questa validazione equivale a dei required sui tre campi tramite data annotation
            if (ModelState.IsValid)
            {
                try
                {
                    if (fornitore.IdFornitore == 0)
                    {
                        Context.Fornitori.Add(fornitore);
                    }
                    else
                    {
                        Context.Entry(fornitore).State = System.Data.Entity.EntityState.Modified;
                    }
                    Context.SaveChanges();
                    success = true;
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Errore", "Errore generico : " + ex.Message);
                }
            }

            if (success)
            {
                //Se ho successo, bisogna che aggiorno la tabella dei fornitori
                FornitoriIndexViewModel vm = FornitoriIndexViewModel.Load(Context, paginaCorrente, PageSize);
                return(Json(new
                {
                    Success = true,
                    Html = RenderRazorViewToString("_List", vm),
                    TargetId = "table_container",
                    CloseDialog = true
                }, JsonRequestBehavior.DenyGet));
                // PartialView("_List",vm);
            }

            FornitoriEditViewModel vmErrore = new FornitoriEditViewModel();

            vmErrore.Fornitore = fornitore;
            return(Json(new
            {
                Success = false,
                Html = RenderRazorViewToString("_Edit", vmErrore),
                TargetId = "modalContent",
                CloseDialog = false
            }, JsonRequestBehavior.DenyGet));
        }