public async Task <IActionResult> Inscricoes([Bind("Id,Nome,Apelido,Morada,CC,Localidade,DataNascimento,ImageFile")] InscricoesViewModel view)
        {
            if (ModelState.IsValid)
            {
                var path = string.Empty;

                if (view.ImageFile != null && view.ImageFile.Length > 0)
                {
                    var guid = Guid.NewGuid().ToString();
                    var file = $"{guid}.jpg";

                    path = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot\\images\\inscricoes",
                        file);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await view.ImageFile.CopyToAsync(stream);
                    }

                    path = $"~/images/inscricoes/{file}";
                }

                var inscricoes = this.ToInscricoes(view, path);

                _context.Add(inscricoes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(view));
        }
Пример #2
0
 private Jogador ToJogador(InscricoesViewModel view, string path)
 {
     return(new Jogador
     {
         Id = view.Id,
         ImageURL = path,
         Nome = view.Nome,
         Apelido = view.Apelido,
         Morada = view.Morada,
         Localidade = view.Localidade,
         CartaoCidadao = view.CartaoCidadao,
         DataNascimento = view.DataNascimento
     });
 }
 private Inscricoes ToInscricoes(InscricoesViewModel view, string path)
 {
     return(new Inscricoes
     {
         Id = view.Id,
         Nome = view.Nome,
         Apelido = view.Apelido,
         Morada = view.Morada,
         CC = view.CC,
         Localidade = view.Localidade,
         DataNascimento = view.DataNascimento,
         ImageUrl = path,
         User = view.User,
     });
 }