public ActionResult Create([Bind(Include = "ID_AUTO,ID_MODELO,PATENTE,ANO,COLOR,OBSERVACION")] AUTOS aUTOS) { if (ModelState.IsValid) { if (aUTOS.ANO < 1990) { aUTOS.OBSERVACION = "ANTIGUO"; } else { aUTOS.OBSERVACION = "SIN OBSERVACIONES"; } var patejemplo = db.AUTOS.Where(x => x.PATENTE == aUTOS.PATENTE); if (patejemplo == null || patejemplo.Count() == 0) { int datefecha = DateTime.Now.Year; Console.WriteLine(datefecha); if (aUTOS.ANO > (datefecha + 2)) { ViewBag.ID_MODELO = new SelectList(db.MODELO, "ID_MODELO", "DESCRIPCION_MODELO", aUTOS.ID_MODELO); ModelState.AddModelError("ANO", "El año no puede ser superior a " + (datefecha + 2)); return(View(aUTOS)); } else { db.AUTOS.Add(aUTOS); db.SaveChanges(); return(RedirectToAction("Index")); } } else { int datefecha = DateTime.Now.Year; Console.WriteLine(datefecha); if (aUTOS.ANO > (datefecha + 2)) { ViewBag.ID_MODELO = new SelectList(db.MODELO, "ID_MODELO", "DESCRIPCION_MODELO", aUTOS.ID_MODELO); ModelState.AddModelError("ANO", "El año no puede ser superior a " + (datefecha + 2)); ModelState.AddModelError("PATENTE", "Patente ya registrada"); return(View(aUTOS)); } else { ViewBag.ID_MODELO = new SelectList(db.MODELO, "ID_MODELO", "DESCRIPCION_MODELO", aUTOS.ID_MODELO); ModelState.AddModelError("PATENTE", "Patente ya registrada"); } } } return(View(aUTOS)); }
public ActionResult Register(Registration_Model registerDetails) { //We check if the model state is valid or not. We have used DataAnnotation attributes. //If any form value fails the DataAnnotation validation the model state becomes invalid. if (ModelState.IsValid) { //create database context using Entity framework using (var databaseContext = new testEntities2()) { //If the model state is valid i.e. the form values passed the validation then we are storing the User's details in DB. RegisterUser reglog = new RegisterUser(); //Save all details in RegitserUser object reglog.FirstName = registerDetails.FirstName; reglog.LastName = registerDetails.LastName; reglog.Email = registerDetails.Email; reglog.Password = registerDetails.Password; //Calling the SaveDetails method which saves the details. databaseContext.RegisterUsers.Add(reglog); databaseContext.SaveChanges(); } ViewBag.Message = "User Details Saved"; return(View("Index")); } else { //If the validation fails, we are returning the model object with errors to the view, which will display the error messages. return(View("Register", registerDetails)); } }
public ActionResult Update(int id, ProductDetail model) { using (var context = new testEntities2()) { // Use of lambda expression to access // particular record from a database var data = context.ProductDetails.FirstOrDefault(x => x.Product_id == id); // Checking if any such record exist if (data != null) { data.Product_name = model.Product_name; data.Product_price = model.Product_price; data.Product_type = model.Product_type; data.Product_weight = model.Product_weight; data.Product_description = model.Product_description; context.SaveChanges(); // It will redirect to // the Read method return(RedirectToAction("Index")); } else { return(View()); } } }
public ActionResult Create([Bind(Include = "INGR_ID,ID_AUTO,INGR_FECHA_INGRESO,INGR_RUT_CLIENTE")] INGRESA_VEHICULO iNGRESA_VEHICULO, string[] servicios) { if (ModelState.IsValid) { foreach (string value in servicios) { SERVICIOS servi = new SERVICIOS(); int idaux = int.Parse(value); servi = db.SERVICIOS.Where(x => x.SERVI_ID == idaux).First(); iNGRESA_VEHICULO.SERVICIOS.Add(servi); } db.INGRESA_VEHICULO.Add(iNGRESA_VEHICULO); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ID_AUTO = new SelectList(db.AUTOS, "ID_AUTO", "ID_MODELO", iNGRESA_VEHICULO.ID_AUTO); return(View(iNGRESA_VEHICULO)); }
public ActionResult Create([Bind(Include = "ID_MARCA,DESCRIPCION")] MARCA mARCA) { if (ModelState.IsValid) { var patejemplo = db.MARCA.Where(x => x.DESCRIPCION == mARCA.DESCRIPCION); if (patejemplo == null || patejemplo.Count() == 0) { db.MARCA.Add(mARCA); db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.ID_MARCA = new SelectList(db.MARCA, "ID_MARCA", "DESCRIPCION", mARCA.ID_MARCA); ModelState.AddModelError("DESCRIPCION", "Marca ya registrada"); return(View(mARCA)); } } return(View(mARCA)); }
public ActionResult Create([Bind(Include = "ID_MODELO,ID_MARCA,DESCRIPCION_MODELO")] MODELO mODELO) { if (ModelState.IsValid) { var patejemplo = db.MODELO.Where(x => x.DESCRIPCION_MODELO == mODELO.DESCRIPCION_MODELO); if (patejemplo == null || patejemplo.Count() == 0) { db.MODELO.Add(mODELO); db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.ID_MARCA = new SelectList(db.MARCA, "ID_MARCA", "DESCRIPCION", mODELO.ID_MARCA); ModelState.AddModelError("DESCRIPCION_MODELO", "Modelo ya registrado"); return(View(mODELO)); } } ViewBag.ID_MARCA = new SelectList(db.MARCA, "ID_MARCA", "DESCRIPCION", mODELO.ID_MARCA); return(View(mODELO)); }
public ActionResult Delete(int id, ProductDetail model) { using (var context = new testEntities2()) { var data = context.ProductDetails.FirstOrDefault(x => x.Product_id == id); if (data != null) { data.Product_name = model.Product_name; data.Product_price = model.Product_price; data.Product_type = model.Product_type; data.Product_weight = model.Product_weight; data.Product_description = model.Product_description; context.ProductDetails.Remove(data); context.SaveChanges(); return(RedirectToAction("Index")); } else { return(View()); } } }
public ActionResult Product(ProductDetail proddetail) { try { if (ModelState.IsValid) { using (var databaseContext = new testEntities2()) { string myDate = DateTime.Now.ToString("yyyyMMdd"); string root = @"C:\Users\SowmyaNelakurthi\Desktop\DotNet_Interview_Prepare\Practise\Products_Site\Products_Site\Automated_Files\" + "Folder" + myDate; // If directory does not exist, create it. if (!Directory.Exists(root)) { Directory.CreateDirectory(root); proddetail.Email = Session["Email"].ToString(); databaseContext.ProductDetails.Add(proddetail); databaseContext.SaveChanges(); string path = Server.MapPath("~/Automated_Files/" + "Folder" + myDate + "/" + proddetail.Product_id + "file"); using (StreamWriter sw = System.IO.File.CreateText(path)) { sw.WriteLine(proddetail.Product_id + ": This is the ID"); sw.WriteLine(proddetail.Email + ": This is the Email ID"); sw.WriteLine(proddetail.Product_name + ": This is the Product Name"); sw.WriteLine(proddetail.Product_price + ": This is the Product price "); sw.WriteLine(proddetail.Product_type + ": This is the Product type"); sw.WriteLine(proddetail.Product_weight + ": This is the Product weight"); sw.WriteLine(proddetail.Product_description + ": This is the Product Description"); } File(path, "text/plain", proddetail.Product_id.ToString()); } else { proddetail.Email = Session["Email"].ToString(); databaseContext.ProductDetails.Add(proddetail); databaseContext.SaveChanges(); string path = Server.MapPath("~/Automated_Files/" + "Folder" + myDate + "/" + proddetail.Product_id + "file"); using (StreamWriter sw = System.IO.File.CreateText(path)) { sw.WriteLine(proddetail.Product_id + ": This is the ID"); sw.WriteLine(proddetail.Email + ": This is the Email ID"); sw.WriteLine(proddetail.Product_name + ": This is the Product Name"); sw.WriteLine(proddetail.Product_price + ": This is the Product price "); sw.WriteLine(proddetail.Product_type + ": This is the Product type"); sw.WriteLine(proddetail.Product_weight + ": This is the Product weight"); sw.WriteLine(proddetail.Product_description + ": This is the Product Description"); } File(path, "text/plain", proddetail.Product_id.ToString()); } } ViewBag.Message = "Product Details Saved"; return(View("Index")); } else { //If the validation fails, we are returning the model object with errors to the view, which will display the error messages. return(View("Register")); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } }