示例#1
0
        public async Task <IActionResult> Add(MemberAddModel model)
        {
            if (ModelState.IsValid)
            {
                Member member = new Member();
                member.Name = model.Name;
                await _context.Members.AddAsync(member);

                List <Language> languages = await _context.Languages.ToListAsync();

                for (int i = 0; i < languages.Count; i++)
                {
                    if (model.Positions[i] != null)
                    {
                        MemberLanguage memberLanguage = new MemberLanguage()
                        {
                            MemberId   = member.Id,
                            Position   = model.Positions[i],
                            LanguageId = languages[i].Id
                        };
                        await _context.MemberLanguages.AddAsync(memberLanguage);
                    }
                }

                foreach (string file in model.Photos)
                {
                    member.Photo = file;
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(RedirectToAction(nameof(Add)));
            }
        }
示例#2
0
 public void AddNewMember(Member newMember, MemberLanguage language)
 {
     Add(newMember);
     Execute("INSERT INTO MemberOption (MemberId) VALUES ({0})", newMember.Id);
     Execute("UPDATE MemberOption SET Language = {0} WHERE MemberId ={1}", (int)language, newMember.Id);
 }
示例#3
0
        public async Task <IActionResult> Edit(MemberAddModel model, int memberId)
        {
            if (!model.Positions.IsNull() && model.Name != null)
            {
                Member member = await _context.Members.Where(s => s.Id == memberId).FirstOrDefaultAsync();

                member.Name = model.Name;
                List <Language> languages = await _context.Languages.ToListAsync();

                for (int i = 0; i < languages.Count; i++)
                {
                    MemberLanguage MemberLanguage = await _context.MemberLanguages
                                                    .Where(sl => sl.MemberId == memberId &&
                                                           sl.LanguageId == languages[i].Id)
                                                    .FirstOrDefaultAsync();

                    if (MemberLanguage == null)
                    {
                        MemberLanguage = new MemberLanguage()
                        {
                            MemberId   = memberId,
                            LanguageId = languages[i].Id,
                            Position   = model.Positions[i],
                        };

                        await _context.MemberLanguages.AddAsync(MemberLanguage);
                    }
                    else
                    {
                        MemberLanguage.Position = model.Positions[i];
                    }
                }

                if (!model.DeletePhotos.IsNull() && !model.DeletePhotos.IsNullOrEmpty())
                {
                    foreach (string deletePhoto in model.DeletePhotos)
                    {
                        if (member.Photo == deletePhoto)
                        {
                            string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads", "Members", deletePhoto);
                            if (System.IO.File.Exists(path))
                            {
                                System.IO.File.Delete(path);
                            }
                            member.Photo = null;
                        }
                    }
                }

                if (!model.Photos.IsNull() && !model.Photos.IsNullOrEmpty())
                {
                    foreach (string addPhoto in model.Photos)
                    {
                        member.Photo = addPhoto;
                    }
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(RedirectToAction(nameof(Edit)));
        }