public ActionResult Create(Restaurante restaurante) { //Añadimos la ciudad y la fecha de creación. restaurante.FechaCreacion = DateTime.Now; restaurante.IdCiudad = 1; if (ModelState.IsValid) { db.Restaurantes.AddObject(restaurante); db.SaveChanges(); //Añadimos el mensaje informando. Message msg = new Message() { type = TypeMessage.Create, text = String.Format(Resources.Mensajes.txtRestaurantAdded, restaurante.Nombre) }; ViewBag.Message = msg; #region Añadimos las columnas. ColumnModel[] columns = GetColumnsForRestaurant(); ViewBag.Column1 = columns[0]; ViewBag.Column2 = columns[1]; ViewBag.Column3 = columns[2]; #endregion return View("Index"); } ViewBag.IdCiudad = new SelectList(db.Ciudades, "Id", "Nombre", restaurante.IdCiudad); return View(restaurante); }
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); }