Пример #1
0
        // GET: Admin/Frases/Edit/5
        public ActionResult Edit(int id, string src, string textoBusqueda, int?temaId, int?subtemaId, int?gramaticaId, int?Pagina)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Frase Frase = db.Frases.Include(fr => fr.Vocabularios).FirstOrDefault(a => a.FraseId == id);

            if (Frase == null)
            {
                return(HttpNotFound());
            }


            FrasesEditViewModel viewModel = new FrasesEditViewModel();
            var gramatica = db.Gramaticas.Include(s => s.SubTema.Tema).SingleOrDefault(s => s.GramaticaId == Frase.GramaticaId);

            viewModel.GramaticaId     = gramatica.GramaticaId;
            viewModel.GramaticaTitulo = gramatica.Titulo;
            viewModel.Frase           = Frase;
            viewModel.SubTema         = gramatica.SubTema;
            viewModel.src             = src;
            viewModel.InicializarDesplegables();

            //Mantener busqueda
            viewModel.TextoBusqueda       = textoBusqueda;
            viewModel.TemaIdBusqueda      = temaId;
            viewModel.SubtemaIdBusqueda   = subtemaId;
            viewModel.GramaticaIdBusqueda = gramaticaId;
            viewModel.PaginaBusqueda      = Pagina;



            viewModel.VocabularioIds = string.Join("#", Frase.Vocabularios.Select(f => f.VocabularioId).ToArray());
            return(View(viewModel));
        }
Пример #2
0
        public ActionResult Edit(FrasesEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (viewModel.AudioFile_es != null)
                {
                    string fullPath = Request.MapPath("~/media/upload/frases_mp3/" + viewModel.Frase.FicheroAudio_es);
                    if (System.IO.File.Exists(fullPath))
                    {
                        System.IO.File.Delete(fullPath);
                    }
                    viewModel.Frase.FicheroAudio_es = viewModel.Frase.FraseId + "_es.mp3";

                    string nameAndLocation = "~/media/upload/frases_mp3/" + viewModel.Frase.FicheroAudio_es;
                    viewModel.AudioFile_es.SaveAs(Server.MapPath(nameAndLocation));

                    db.Entry(viewModel.Frase).State = EntityState.Modified;
                    db.SaveChanges();
                }
                if (viewModel.AudioFile_en != null)
                {
                    string fullPath = Request.MapPath("~/media/upload/frases_mp3/" + viewModel.Frase.FicheroAudio_en);
                    if (System.IO.File.Exists(fullPath))
                    {
                        System.IO.File.Delete(fullPath);
                    }
                    viewModel.Frase.FicheroAudio_en = viewModel.Frase.FraseId + "_en.mp3";

                    string nameAndLocation = "~/media/upload/frases_mp3/" + viewModel.Frase.FicheroAudio_en;
                    viewModel.AudioFile_en.SaveAs(Server.MapPath(nameAndLocation));

                    db.Entry(viewModel.Frase).State = EntityState.Modified;
                    db.SaveChanges();
                }

                db.Entry(viewModel.Frase).State = EntityState.Modified;
                db.SaveChanges();

                Frase Frase             = db.Frases.Include(fr => fr.Vocabularios).FirstOrDefault(a => a.FraseId == viewModel.Frase.FraseId);
                var   vocabulariosFrase = Frase.Vocabularios.ToList();
                foreach (var vocabulario in vocabulariosFrase)
                {
                    Frase.Vocabularios.Remove(vocabulario);
                }


                //VocabularioIds
                if (!string.IsNullOrWhiteSpace(viewModel.VocabularioIds))
                {
                    if (viewModel.VocabularioIds.StartsWith("#"))
                    {
                        viewModel.VocabularioIds = viewModel.VocabularioIds.Substring(1, viewModel.VocabularioIds.Length - 1);
                    }
                    var arrVocabularioIds = viewModel.VocabularioIds.Split('#');

                    foreach (var vocabularioId in arrVocabularioIds)
                    {
                        var vocabulario = db.Vocabularios.First(vo => vo.VocabularioId.ToString() == vocabularioId);
                        if (!Frase.Vocabularios.Contains(vocabulario))
                        {
                            Frase.Vocabularios.Add(vocabulario);
                        }
                    }
                }
                db.Entry(Frase).State = EntityState.Modified;
                db.SaveChanges();

                if (viewModel.src == "Gestor")
                {
                    return(RedirectToAction("Gestor", "Frases", new { textoBusqueda = viewModel.TextoBusqueda, temaId = viewModel.TemaIdBusqueda,
                                                                      subtemaId = viewModel.SubtemaIdBusqueda, gramaticaId = viewModel.GramaticaIdBusqueda, pagina = viewModel.PaginaBusqueda }));
                }
                return(RedirectToAction("Index", "Frases", new { id = viewModel.Frase.GramaticaId }));
            }

            var gramatica = db.Gramaticas.Include(s => s.SubTema.Tema).SingleOrDefault(s => s.GramaticaId == viewModel.Frase.GramaticaId);

            viewModel.SubTema = gramatica.SubTema;
            viewModel.InicializarDesplegables();
            return(View(viewModel));
        }