コード例 #1
0
        public ActionResult RegisterProfile(RegisterProfileViewModel model)
        {
            if (ModelState.IsValid)
            {
                var ctx         = new AppDbContext();
                var currentUser = User.Identity.GetUserId();
                var profile     = new Profile {
                    Active = true, Förnamn = model.Förnamn, Efternamn = model.Efternamn, Födelseår = model.Födelseår, Description = model.Description, ApplicationUser = currentUser
                };
                ctx.Profiles.Add(profile);
                ctx.SaveChanges();

                return(RedirectToAction("Index", "Home")); //Ange vart vi hamnar efter vi är klara.
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
コード例 #2
0
        public async Task <ActionResult> Register(RegisterProfileViewModel model)
        {
            ViewBag.Organizations = new SelectList(new SharedController().GetOrganizations(null, true).ToList(), "Id", "Name", string.Empty);
            if (ModelState.IsValid)
            {
                try
                {
                    string email = StringHelper.KillChars(Request.Form["Email"]);
                    string name  = StringHelper.KillChars(Request.Form["Name"]);
                    //Kiểm tra chưa nhập họ tên và email
                    if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(name))
                    {
                        return(View(model));
                    }
                    //Kiểm tra trùng email
                    bool exists = await _repository.GetRepository <Account>().AnyAsync(o => o.Email == email);

                    bool exists2 = await _repository.GetRepository <Profile>().AnyAsync(o => o.Email == email);

                    if (exists || exists2)
                    {
                        ModelState.AddModelError(string.Empty, "Địa chỉ email đã tồn tại!");
                        ModelState.AddModelError("Email", "Địa chỉ email này đã được sử dụng cho tài khoản khác!");
                        return(View(model));
                    }
                    //Kiểm tra chưa chọn trường
                    string[] organizations = null;
                    if (Request.Form["OrganizationId"] != null)
                    {
                        organizations = Request.Form["OrganizationId"].Split(',');
                    }
                    if (organizations == null || organizations.Length == 0)
                    {
                        ModelState.AddModelError(string.Empty, "Vui lòng chọn trường!");
                        return(View(model));
                    }

                    string dob              = StringHelper.KillChars(Request.Form["DateOfBirth"]);
                    string sex              = StringHelper.KillChars(Request.Form["Sex"]);
                    string cellphone        = StringHelper.KillChars(Request.Form["CellPhone"]);
                    string nativeland       = StringHelper.KillChars(Request.Form["NativeLand"]);
                    string currentresidence = StringHelper.KillChars(Request.Form["CurrentResidence"]);
                    string extrainfomation  = StringHelper.KillChars(Request.Form["ExtraInfomation"]);


                    //string[] amounts = Request.Form.GetValues("Amount");
                    //string[] amounts = Request.Form["Amount"].Split(new char[] { ',' });

                    Profile profile = new Profile();
                    profile.Name             = name;
                    profile.Email            = email;
                    profile.Sex              = string.IsNullOrEmpty(sex) ? false : Convert.ToBoolean(sex);
                    profile.CellPhone        = cellphone;
                    profile.NativeLand       = nativeland;
                    profile.CurrentResidence = currentresidence;
                    profile.ExtraInfomation  = extrainfomation;

                    profile.Status     = 1;
                    profile.CreateDate = DateTime.Now;
                    profile.CreateBy   = 0;

                    if (!string.IsNullOrEmpty(dob))
                    {
                        try
                        {
                            DateTime date = DateTime.ParseExact(dob, "dd/MM/yyyy", null);
                            profile.DateOfBirth = date;
                        }
                        catch
                        {
                            profile.DateOfBirth = null;
                        }
                    }
                    int result = await _repository.GetRepository <Profile>().CreateAsync(profile, 0);

                    if (result > 0)
                    {
                        string[] startyears    = Request.Form.GetValues("StartYear");
                        string[] graduateyears = Request.Form.GetValues("GraduateYear");
                        string[] majorsids     = Request.Form.GetValues("MajorsId");
                        string[] courseids     = Request.Form.GetValues("CourseId");

                        var profileOrganizations = new List <ProfileOrganization>();
                        for (int i = 0; i < organizations.Length; i++)
                        //Response.Write(string.Format("Value of Item{0} is {1}", i, organizations[i]));
                        {
                            if (!string.IsNullOrEmpty(organizations[i]))
                            {
                                long x1             = profile.Id;
                                long OrganizationId = Convert.ToInt64(organizations[i]);
                                int? StartYear      = string.IsNullOrEmpty(startyears[i]) ? (int?)null : Convert.ToInt16(startyears[i]);
                                int? GraduateYear   = string.IsNullOrEmpty(graduateyears[i]) ? (int?)null : Convert.ToInt16(graduateyears[i]);
                                long?MajorsId       = string.IsNullOrEmpty(majorsids[i]) ? (long?)null : Convert.ToInt64(majorsids[i]);
                                long?CourseId       = string.IsNullOrEmpty(courseids[i]) ? (long?)null : Convert.ToInt64(courseids[i]);
                                profileOrganizations.Add(new ProfileOrganization()
                                {
                                    ProfileId      = profile.Id,
                                    OrganizationId = Convert.ToInt64(organizations[i]),
                                    StartYear      = string.IsNullOrEmpty(startyears[i]) ? (int?)null : Convert.ToInt16(startyears[i]),
                                    GraduateYear   = string.IsNullOrEmpty(graduateyears[i]) ? (int?)null : Convert.ToInt16(graduateyears[i]),
                                    MajorsId       = string.IsNullOrEmpty(majorsids[i]) ? (long?)null : Convert.ToInt64(majorsids[i]),
                                    CourseId       = string.IsNullOrEmpty(courseids[i]) ? (long?)null : Convert.ToInt64(courseids[i])
                                });
                            }
                        }
                        if (profileOrganizations != null && profileOrganizations.Any())
                        {
                            int result2 = await _repository.GetRepository <ProfileOrganization>().CreateAsync(profileOrganizations, 0);
                        }
                        TempData["Success"] = true;
                        return(RedirectToRoute("FrontEndProfileRegister"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Vui lòng nhập chính xác các thông tin hồ sơ!");
                        return(View(model));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, "Vui lòng nhập chính xác các thông tin hồ sơ!");
                    return(View(model));
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Vui lòng nhập chính xác các thông tin hồ sơ!");
                return(View(model));
            }
        }