public ActionResult Create(Department department) { if (ModelState.IsValid) { db.Departments.Add(department); try { db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", department.CountryId); return(View(department)); }
public ActionResult Create(Country country) { if (ModelState.IsValid) { db.Countries.Add(country); try { db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } return(View(country)); }
public ActionResult Edit(Sms sms) { if (ModelState.IsValid) { try { //Logica similar al envío para reenviar un mensaje nuevamente db.Entry(sms).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError(string.Empty, "Se presentó un error al enviar el conjunto de SMS. Por favor verifique el mensaje y los destinatarios"); } } return(View(sms)); }
private void SendEmail(string to, string from, string password, string subject, string body) { using (MailMessage mm = new MailMessage(from, to)) { mm.Subject = subject; mm.Body = body; mm.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(from, password); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; using (var context = new WebApiPruebaContext()) { try { smtp.Send(mm); var emailStatus = new EmailSendStatus { Email = to, Send = true }; context.EmailSendStatus.Add(emailStatus); context.SaveChanges(); } catch (Exception ex) { var emailStatus = new EmailSendStatus { Email = to, Send = false }; context.EmailSendStatus.Add(emailStatus); context.SaveChanges(); } } } }
public ActionResult Create(Link link, string newWorkPlace) { if (ModelState.IsValid) { if (link.WorkPlaceId == 9999) { if (!string.IsNullOrEmpty(newWorkPlace)) { var workPlace = new WorkPlace { Name = newWorkPlace, }; db.WorkPlaces.Add(workPlace); try { db.SaveChanges(); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un lugar de trabajo con ese nombre"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", link.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", link.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", link.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", link.DepartmentId); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name", link.CommuneId); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name", link.WorkPlaceId); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", link.VotingPlaceId); return(View(link)); } db.SaveChanges(); link.WorkPlaceId = workPlace.WorkPlaceId; } } db.Links.Add(link); try { db.SaveChanges(); //UsersHelper.CreateUserASP(link.UserName, "Link"); if (link.PhotoFile != null) { var folder = "~/Content/Links"; var file = string.Format("{0}.jpg", link.LinkId); var response = FilesHelper.UploadPhoto(link.PhotoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}", folder, file); link.Photo = pic; db.Entry(link).State = EntityState.Modified; db.SaveChanges(); } } var city = db.Cities.Find(link.CityId); var department = db.Departments.Find(link.DepartmentId); var country = db.Countries.Find(link.CountryId); var comune = db.Communes.Find(link.CommuneId); var votingPlace = db.VotingPlaces.Find(link.VotingPlaceId); var refer = db.Refers.Where(r => r.ReferType == 1 && r.UserId == link.BossId).FirstOrDefault(); var Voter = new Voter { Address = link.Address, BossId = link.BossId, CityId = city.Name, CommuneId = comune.Name, CompanyId = link.CompanyId, CountryId = country.Name, DepartmentId = department.Name, Document = link.Document, FirstName = link.FirstName, LastName = link.LastName, Phone = link.Phone, UserName = link.UserName, VotingPlaceId = votingPlace.Name, userId = 2, ReferId = refer.ReferId, PerfilId = 1, Fname = string.Format("{0} {1}", link.FirstName, link.LastName), Barrio = link.Barrio, DateBorn = link.DateBorn, Profesion = link.Profesion, }; db.Voters.Add(Voter); db.SaveChanges(); var Refer = new Refer { ReferType = 2, UserId = link.LinkId, FullName = link.FullName, Active = 1 }; db.Refers.Add(Refer); db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", link.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", link.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", link.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", link.DepartmentId); ViewBag.BossId = new SelectList( CombosHelper.GetBosses(link.CompanyId), "BossId", "FullName", link.BossId); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name", link.CommuneId); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name", link.WorkPlaceId); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", link.VotingPlaceId); return(View(link)); }
public ActionResult Create(Voter voter) { if (ModelState.IsValid) { var refer = db.Refers.Find(voter.ReferId); var city = db.Cities.Find(Convert.ToInt32(voter.CityId)); var department = db.Departments.Find(Convert.ToInt32(voter.DepartmentId)); var country = db.Countries.Find(Convert.ToInt32(voter.CountryId)); var comune = db.Communes.Find(Convert.ToInt32(voter.CommuneId)); var votingPlace = db.VotingPlaces.Find(Convert.ToInt32(voter.VotingPlaceId)); if (voter.PerfilId == 1) { voter.BossId = refer.UserId; } if (voter.PerfilId == 2) { voter.LinkId = refer.UserId; } if (voter.PerfilId == 3) { voter.CoordinatorId = refer.UserId; } if (voter.PerfilId == 4) { voter.LeaderId = refer.UserId; } voter.userId = 5; voter.ReferId = refer.ReferId; voter.Fname = string.Format("{0} {1}", voter.FirstName, voter.LastName); voter.CityId = city.Name; voter.DepartmentId = department.Name; voter.CountryId = country.Name; voter.CommuneId = comune.Name; voter.VotingPlaceId = votingPlace.Name; db.Voters.Add(voter); try { db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CityId = new SelectList(CombosHelper.GetCities(), "CityId", "Name", voter.CityId); ViewBag.CommuneId = new SelectList(CombosHelper.GetCommunes(), "CommuneId", "Name", voter.CommuneId); ViewBag.CountryId = new SelectList(CombosHelper.GetCountries(), "CountryId", "Name", voter.CountryId); ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartments(), "DepartmentId", "Name", voter.DepartmentId); ViewBag.VotingPlaceId = new SelectList(CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", voter.VotingPlaceId); ViewBag.PerfilId = new SelectList( CombosHelper.GetUser(), "userId", "name", voter.PerfilId); ViewBag.ReferId = new SelectList( CombosHelper.GetRefer(), "ReferId", "FullName", voter.ReferId); return(View(voter)); }
public ActionResult Create(Company company) { if (ModelState.IsValid) { db.Companies.Add(company); try { db.SaveChanges(); if (company.LogoFile != null) { var folder = "~/Content/Logos"; var file = string.Format("{0}_{1}.jpg", company.CompanyId, company.Name); var response = FilesHelper.UploadPhoto(company.LogoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}", folder, file); company.Logo = pic; db.Entry(company).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", company.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", company.CityId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", company.DepartmentId); return(View(company)); }
public ActionResult Importexcel1() { if (Request.Files["FileUpload1"].ContentLength > 0) { string extension = System.IO.Path.GetExtension(Request.Files["FileUpload1"].FileName).ToLower(); string connString = ""; string[] validFileTypes = { ".xls", ".xlsx", ".csv" }; string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/Uploads"), Request.Files["FileUpload1"].FileName); if (!Directory.Exists(path1)) { Directory.CreateDirectory(Server.MapPath("~/Content/Uploads")); } if (validFileTypes.Contains(extension)) { if (System.IO.File.Exists(path1)) { System.IO.File.Delete(path1); } Request.Files["FileUpload1"].SaveAs(path1); if (extension == ".csv") { DataTable dt = Utility.ConvertCSVtoDataTable(path1); ViewBag.Data = dt; } //Connection String to Excel Workbook else if (extension.Trim() == ".xls") { connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path1 + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; DataTable dt = Utility.ConvertXSLXtoDataTable(path1, connString); int contador = 0; foreach (DataRow row in dt.Rows) { var products = new Product { IdProducto = Convert.ToInt32(row[0].ToString()), NombreProducto = row[1].ToString(), Proveedor = row[2].ToString(), Categoria = row[3].ToString(), CantidadPorUnidad = row[4].ToString(), PrecioUnidad = row[5].ToString(), UnidadesEnExistencia = Convert.ToInt32(row[6].ToString()), }; db.Products.Add(products); db.SaveChanges(); contador = dt.Rows.Count; } if (contador > 0) { ViewBag.Message = "Se han ingresado correctamente " + contador + " Registros"; } ViewBag.Data = dt; } else if (extension.Trim() == ".xlsx") { connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; DataTable dt = Utility.ConvertXSLXtoDataTable(path1, connString); ViewBag.Data = dt; } } else { ViewBag.Error = "Please Upload Files in .xls, .xlsx or .csv format"; } } return(View()); }
public ActionResult Create(User user, string rol) { if (ModelState.IsValid) { db.Users.Add(user); try { var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(userContext)); var UserASP = userManager.FindByEmail(user.UserName); if (UserASP == null) { db.SaveChanges(); if (rol == "1") { UsersHelper.CreateUserASP(user.UserName, "User"); } if (rol == "2") { UsersHelper.CreateUserASP(user.UserName, "Digitador"); } if (rol == "3") { UsersHelper.CreateUserASP(user.UserName, "Reunion"); } if (rol == "4") { UsersHelper.CreateUserASP(user.UserName, "Secretario"); } if (user.PhotoFile != null) { var folder = "~/Content/Users"; var file = string.Format("{0}.jpg", user.UserId); var response = FilesHelper.UploadPhoto(user.PhotoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}", folder, file); user.Photo = pic; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } else { ModelState.AddModelError(string.Empty, "Ya existe un usuario con ese correo electrónico"); List <SelectListItem> mn2 = new List <SelectListItem>(); mn2.Add(new SelectListItem { Text = "Hombre", Value = "1" }); mn2.Add(new SelectListItem { Text = "Mujer", Value = "2" }); ViewData["genero"] = mn2; ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", user.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", user.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", user.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", user.DepartmentId); List <SelectListItem> muser2 = new List <SelectListItem>(); muser2.Add(new SelectListItem { Text = "Seleccione un rol", Value = "0" }); muser2.Add(new SelectListItem { Text = "Administrador", Value = "1" }); muser2.Add(new SelectListItem { Text = "Digitador", Value = "2" }); muser2.Add(new SelectListItem { Text = "Reuniones", Value = "3" }); muser2.Add(new SelectListItem { Text = "Secretario", Value = "4" }); ViewData["rol"] = muser2; return(View(user)); } } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", user.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", user.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", user.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", user.DepartmentId); List <SelectListItem> muser = new List <SelectListItem>(); muser.Add(new SelectListItem { Text = "Seleccione un rol", Value = "0" }); muser.Add(new SelectListItem { Text = "Administrador", Value = "1" }); muser.Add(new SelectListItem { Text = "Digitador", Value = "2" }); muser.Add(new SelectListItem { Text = "Reuniones", Value = "3" }); muser.Add(new SelectListItem { Text = "Secretario", Value = "4" }); ViewData["rol"] = muser; List <SelectListItem> mn = new List <SelectListItem>(); mn.Add(new SelectListItem { Text = "Hombre", Value = "1" }); mn.Add(new SelectListItem { Text = "Mujer", Value = "2" }); ViewData["genero"] = mn; return(View(user)); }