public IActionResult Post([FromBody] FormData item)
        {
            if (item == null)
            {
                return(BadRequest());
            }
            string userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

            item.UserId = userId;

            _context.FormDatas.Add(item);
            _context.SaveChanges();

            return(CreatedAtRoute("GetById", new { id = item.Id }, item));
        }
        public ActionResult Create(FormCollection formCollection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    FormModel formModel = new FormModel();
                    formModel.UserName     = formCollection["UserName"].ToLower();
                    formModel.Email        = formCollection["Email"];
                    formModel.DOB          = DateTime.Parse(formCollection["DOB"]);
                    formModel.Mobilenumber = (formCollection["Mobilenumber"]);

                    var searchData = fb.FormTable.Where(x => x.UserName == formModel.UserName).SingleOrDefault();
                    if (searchData != null)
                    {
                        TempData["IsEmails"] = "User already exist ! Choose other";
                        return(RedirectToAction("Index"));
                    }
                    //formModel.IsEmail =bool.Parse( formCollection["IsEmail"]);
                    fb.FormTable.Add(formModel);
                    fb.SaveChanges();

                    using (MailMessage mm = new MailMessage())
                    {
                        mm.From = new MailAddress(ConfigurationManager.AppSettings["fromEmail"].ToString());
                        mm.To.Add(formModel.Email);
                        mm.Subject = "Account Activation";
                        string body = "Hello " + formModel.UserName + ",";
                        body         += "<br /><br /><b>Your registration has been successful! </b><br />";
                        body         += "<br />Following are your registration details <br /><br />UserName : "******"<br />Email : " + formModel.Email + "<br /> DOB : " + formModel.DOB + "<br /> MobileNumber : " + formModel.Mobilenumber + "";
                        body         += "<br /><br />Thanks";
                        mm.Body       = body;
                        mm.IsBodyHtml = true;

                        SmtpClient smtp = new SmtpClient();
                        smtp.Host                  = "smtp.gmail.com";
                        smtp.EnableSsl             = true;
                        smtp.DeliveryMethod        = SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        NetworkCredential NetworkCred = new NetworkCredential(ConfigurationManager.AppSettings["fromEmail"].ToString(), ConfigurationManager.AppSettings["EMAILPASSWORD"].ToString());

                        smtp.Credentials = NetworkCred;
                        smtp.Port        = 587;
                        smtp.Send(mm);

                        TempData["IsEmails"] = "Your mail sent successfully";
                    }
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                TempData["IsEmails"] = "Invalid mail";
                return(RedirectToAction("Index"));
            }
        }
 public FormDataController(FormDataContext context)
 {
     _context = context;
     if (_context.FormDatas.Count() == 0)
     {
         _context.FormDatas.Add(new FormData {
             Mood = "Happy"
         });
         _context.SaveChanges();
     }
 }
Ejemplo n.º 4
0
        public ActionResult Index(FormViewModel model)
        {
            ContactData data = new ContactData
            {
                ContactDataId = model.ContactDataId,
                Name          = model.Name,
                Surname       = model.Surname,
                Comment       = model.Comment
            };

            if (ModelState.IsValid)
            {
                db.ContactData.Add(data);
                db.SaveChanges();
                return(RedirectToAction("Finish", "Home"));
            }
            return(View(model));
        }