public ActionResult ViajeAgregar(ViajeCLS oViajeCLS) { if (!ModelState.IsValid) { listarCombos(); return(View(oViajeCLS)); } using (var bd = new BDPasajeEntities()) { Viaje oViaje = new Viaje(); oViaje.IIDLUGARDESTINO = oViajeCLS.iidLugarDestino; oViaje.IIDLUGARORIGEN = oViajeCLS.iidLugarOrigen; oViaje.PRECIO = (decimal)oViajeCLS.precio; oViaje.FECHAVIAJE = oViajeCLS.fechaViaje; oViaje.IIDBUS = oViajeCLS.iidBus; oViaje.NUMEROASIENTOSDISPONIBLES = oViajeCLS.numeroAsientosDisponibles; oViaje.BHABILITADO = 1; bd.Viaje.Add(oViaje); bd.SaveChanges(); } return(RedirectToAction("ViajeView")); }
public ActionResult ViajeEdit(int id) { ViajeCLS oViajeCLS = new ViajeCLS(); listarCombos(); using (var bd = new BDPasajeEntities()) { Viaje oViaje = bd.Viaje.Find(id); oViajeCLS.iidViaje = oViaje.IIDVIAJE; oViajeCLS.iidLugarDestino = (int)oViaje.IIDLUGARDESTINO; oViajeCLS.iidLugarOrigen = (int)oViaje.IIDLUGARORIGEN; oViajeCLS.precio = (double)oViaje.PRECIO; oViajeCLS.fechaViaje = (DateTime)oViaje.FECHAVIAJE; oViajeCLS.iidBus = (int)oViaje.IIDBUS; oViajeCLS.numeroAsientosDisponibles = (int)oViaje.NUMEROASIENTOSDISPONIBLES; } return(View(oViajeCLS)); }
public JsonResult recuperarInfo(int idViaje) { ViajeCLS oViajeCLS = new ViajeCLS(); using (var bd = new BDPasajeEntities()) { Viaje oViaje = bd.Viaje.Where(p => p.IIDVIAJE == idViaje).First(); oViajeCLS.iidViaje = oViaje.IIDVIAJE; oViajeCLS.iidBus = (int)oViaje.IIDBUS; oViajeCLS.iidLugarDestino = (int)oViaje.IIDLUGARDESTINO; oViajeCLS.iidLugarOrigen = (int)oViaje.IIDLUGARORIGEN; oViajeCLS.precio = (int)oViaje.PRECIO; //año-mes-dia (así la pide) //en la bd viene: día-mes-año oViajeCLS.fechaViajeCadena = ((DateTime)oViaje.FECHAVIAJE).ToString("yyyy-MM-dd"); oViajeCLS.numeroAsientosDisponibles = (int)oViaje.NUMEROASIENTOSDISPONIBLES; oViajeCLS.nombreFoto = oViaje.nombrefoto; oViajeCLS.extension = Path.GetExtension(oViaje.nombrefoto); oViajeCLS.fotoRecuperarCadena = Convert.ToBase64String(oViaje.FOTO); } return(Json(oViajeCLS, JsonRequestBehavior.AllowGet)); }
public string Guardar(ViajeCLS oViajeCLS, HttpPostedFileBase foto, int titulo) { string mensaje = ""; try { if (!ModelState.IsValid || (foto == null && titulo == -1)) { //Vamos a obtener los estados de cada propiedad y los mensajes de error (si es que hay). var query = (from state in ModelState.Values from error in state.Errors select error.ErrorMessage).ToList(); if (foto == null && titulo == -1) { oViajeCLS.mensaje = "La foto es obligatoria"; mensaje += "<ul><li> Debe ingresar la foto </li></ul>"; } mensaje += "<ul class='list-group'>"; foreach (var item in query) { mensaje += "<li class = 'list-group-item'>" + item + "</li>"; } mensaje += "</ul>"; } else { byte[] fotoBD = null; if (foto != null) { BinaryReader lector = new BinaryReader(foto.InputStream); fotoBD = lector.ReadBytes((int)foto.ContentLength); } using (var bd = new BDPasajeEntities()) { if (titulo == -1) { Viaje oViaje = new Viaje(); oViaje.IIDBUS = oViajeCLS.iidBus; oViaje.IIDLUGARDESTINO = oViajeCLS.iidLugarDestino; oViaje.IIDLUGARORIGEN = oViajeCLS.iidLugarOrigen; oViaje.PRECIO = oViajeCLS.precio; oViaje.FECHAVIAJE = oViajeCLS.fechaViaje; oViaje.NUMEROASIENTOSDISPONIBLES = oViajeCLS.numeroAsientosDisponibles; oViaje.FOTO = fotoBD; oViaje.nombrefoto = oViajeCLS.nombreFoto; oViaje.BHABILITADO = 1; bd.Viaje.Add(oViaje); mensaje = bd.SaveChanges().ToString(); if (mensaje == "0") { mensaje = ""; } } else { Viaje oViaje = bd.Viaje.Where(p => p.IIDVIAJE == titulo).First(); oViaje.IIDLUGARDESTINO = oViajeCLS.iidLugarDestino; oViaje.IIDLUGARORIGEN = oViajeCLS.iidLugarOrigen; oViaje.PRECIO = oViajeCLS.iidLugarOrigen; oViaje.FECHAVIAJE = oViajeCLS.fechaViaje; oViaje.IIDBUS = oViajeCLS.iidBus; oViaje.NUMEROASIENTOSDISPONIBLES = oViajeCLS.numeroAsientosDisponibles; oViaje.IIDLUGARDESTINO = oViajeCLS.iidLugarDestino; if (foto != null) { oViaje.FOTO = fotoBD; } mensaje = bd.SaveChanges().ToString(); } } } } catch (Exception ex) { mensaje = ""; } return(mensaje); }