public ActionResult Create(Bocadillo bocadillo) { if (ModelState.IsValid) { db.Bocadillos.AddObject(bocadillo); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.IdRestaurante = new SelectList(db.Restaurantes, "Id", "Nombre", bocadillo.IdRestaurante); return View(bocadillo); }
public ActionResult Create(Bocadillo bocadillo, FormCollection collection) { //Rellenamos el idRestaurante. bocadillo.IdRestaurante = Convert.ToInt32(collection["IdRestaurante"]); //Añadimos la fecha actual como creación. bocadillo.FechaCreacion = DateTime.Now; //Comprobamos si el modelo es valido y lo guardamos. if (ModelState.IsValid) { db.Bocadillos.AddObject(bocadillo); db.SaveChanges(); //Recuperamos los ingredientes que se han puesto. String ingredientesStr = collection["hdnTags"]; //Lo separamos los ingredientes, quitando antes la coma del final. ingredientesStr = ingredientesStr.Substring(0, ingredientesStr.Length - 1); List<String> ingredientesList = ingredientesStr.Split(',').ToList(); //Hacemos el manejo necesario con los ingredientes. IngredientesUtilities ingreUtilities = new IngredientesUtilities(); List<BocadilloIngrediente> ingredientesBocadillo = ingreUtilities.ManageIngredientes(ingredientesList, bocadillo.Id); //Agregamos los ingredientes al bocadillo. foreach (BocadilloIngrediente item in ingredientesBocadillo) { bocadillo.BocadilloIngrediente.Add(item); } db.SaveChanges(); //Añadimos el mensaje informando. Message msg = new Message() { type = TypeMessage.Create, text = String.Format(Resources.Mensajes.txtRestaurantAdded, bocadillo.Nombre, bocadillo.Restaurante.Nombre) }; ViewBag.Message = msg; return RedirectToAction("Carta", new { idRestaurante = bocadillo.IdRestaurante }); } //Si hay algún fallo volvemos. ViewData["IdRestaurante"] = collection["IdRestaurante"]; return View(bocadillo); }
/// <summary> /// Create a new Bocadillo object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="idRestaurante">Initial value of the IdRestaurante property.</param> /// <param name="nombre">Initial value of the Nombre property.</param> /// <param name="descripcion">Initial value of the Descripcion property.</param> /// <param name="precio">Initial value of the Precio property.</param> /// <param name="fechaCreacion">Initial value of the FechaCreacion property.</param> public static Bocadillo CreateBocadillo(global::System.Int32 id, global::System.Int32 idRestaurante, global::System.String nombre, global::System.String descripcion, global::System.Decimal precio, global::System.DateTime fechaCreacion) { Bocadillo bocadillo = new Bocadillo(); bocadillo.Id = id; bocadillo.IdRestaurante = idRestaurante; bocadillo.Nombre = nombre; bocadillo.Descripcion = descripcion; bocadillo.Precio = precio; bocadillo.FechaCreacion = fechaCreacion; return bocadillo; }
/// <summary> /// Deprecated Method for adding a new object to the Bocadillos EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToBocadillos(Bocadillo bocadillo) { base.AddObject("Bocadillos", bocadillo); }
public ActionResult Edit(Bocadillo bocadillo) { if (ModelState.IsValid) { db.Bocadillos.Attach(bocadillo); db.ObjectStateManager.ChangeObjectState(bocadillo, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.IdRestaurante = new SelectList(db.Restaurantes, "Id", "Nombre", bocadillo.IdRestaurante); return View(bocadillo); }