Redirect to a page through a post

Asked

Viewed 24 times

0

People needed to do the following I have a page made in pure html and in it I have an area where the user can put his email and ask to register it. Ai wanted to take this email from inoput text and in the post direct to another page where I get the received string and the handle in the controller. but I am not succeeding in including the input value in my url.

<form>
    <section class="call-to-action text-white text-center">
        <div class="overlay"></div>
        <div class="container">
            <div class="row">
                <div class="col-xl-9 mx-auto">
                    <h2 class="mb-4">Quer receber as melhores ofertas dos nossos parceiros? Cadastre-se abaixo!</h2>
                </div>
                <div class="col-md-10 col-lg-8 col-xl-7 mx-auto">
                    <form>
                        <div class="form-row">
                            <div class="col-12 col-md-9 mb-2 mb-md-0">
                                <input type="email" id="email" name="email" class="form-control form-control-lg" placeholder="Digite seu melhor email...">
                            </div>
                            <div class="col-12 col-md-3">
                                <button type="submit" id="enviar" formmethod="post" formaction="../Home/GetEmail/?email=document.getElementById('email').value" class="btn btn-block btn-lg btn-primary">Cadastrar!</button>
         
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
</form>

but when redirecting is like this:

http://localhost:64073/Home/Getemail/? email=Document.getElementById(%27email%27). value

Maybe my way of concatenating is wrong. Some id~How to solve this?

  • See if adding the attribute action="../Home/GetEmail/ in his tag form solves your problem.

1 answer

0


It was actually just a way of concatenating the string into formaction :\

formaction=".. /Home/Getemail/? email=Document.getElementById('email'). value"

it goes through my url: /Home/Getemail/? email=Document.getElementById(%27email%27). value

But the controller can recover the e-mail and then it worked!

        public ActionResult GetEmail(string email)
        {
            EmailRegister emailregister = new EmailRegister();

            if (email != "" && email != null)
            {
                try
                {
                    emailregister.Email = email;
                    emailregister.RegisterDate = DateTime.Now;

                    _emailregisterRepository.InsertEmailRegister(emailregister);
                    _emailregisterRepository.Save();

                    TempData["Message"] = "E-mail cadastrado com sucesso!";

                }
                catch (DataException ex)
                {
                    TempData["Error"] = "Não foi possível salvar as alterações. Tente de novo, e se o problema persistir consulte o administrador do sistema. Erro: " + ex.Message;
                }

            }


            return View("GetEmail");
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.