What’s wrong with this form?

Asked

Viewed 60 times

0

Guys, I have an Asp.net mvc application that will only have a single page, which is index.cshtml. On this page I have a contact form, so he is not sending the captured message to my email, someone would help me solve this, follow below my code.

Index.cshtml - Here is my form

 <div class="w3-col m6">
        <form action="@Url.Action("Index", "Home")" method="post">
            <div class="w3-row-padding" style="margin:0 -16px 8px -16px">

                <div class="w3-half">
                    <div class="form-group">
                        <label for="exampleSelect1">Você aceita essa proposta?</label>
                        <select class="form-control" id="exampleSelect1">
                            <option>Sim</option>
                            <option>Não</option>
                        </select>
                    </div>
                </div>
            </div>

            <input class="w3-input w3-border" type="text" placeholder="Deseja dizer algo?" required name="Mensagem">
            <button class="w3-button w3-black w3-section w3-right" type="submit">ENVIAR</button>
        </form>
    </div> 

Homecontroller.Cs - Here is the logical part that will take care of smtp

 public class HomeController : Controller
    {
        // GET: Home
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string exampleSelect1, string Mensagem)
        {
            //Faça validação adicional nos seus parâmetros de entrada
            if (!string.IsNullOrWhiteSpace(exampleSelect1) && !string.IsNullOrWhiteSpace(Mensagem))
            {
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                client.Host = "email-ssl.com.br ";
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential("[email protected]", "minha_senha");
                MailMessage mail = new MailMessage();
                mail.Sender = new System.Net.Mail.MailAddress("[email protected]");
                mail.From = new MailAddress("Orçamentos");
                mail.To.Add(new MailAddress("[email protected]", "RECEBEDOR"));
                mail.Subject = "Contato";
                mail.Body = "Nome:  " + exampleSelect1 + " <br/> Mensagem : " + Mensagem;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;
                try
                {
                    client.Send(mail);
                }
                catch (System.Exception erro)
                {
                    //trata erro
                }
                finally
                {
                    mail = null;
                }
            }
            //Vai retornar para sua Contatos com o verbo [HttpGet]
            return View();
        }
    }
  • Already tried to pass using viewbags ?

  • @Prostetnicvogonjeltz, could you show me an example of what it would be like? Because I did not try this way, even because I am beginner in this language!

  • https://www.aspsnippets.com/Articles/ASPNet-MVC-HtmlBeginForm-Tutorial-with-example.aspx - Take a look at this link, it might help you to understand another way of doing.

  • 1

    failed to put the attribute name in <select class="form-control" id="exampleSelect1" name='exampleSelect1'>

  • @Leandroangelo was just that, put your comment as an answer for me to approve... thank you!

1 answer

1


You need to include the attribute name in the form elements, it is through it that the Action will recognize as input parameter, in your case it failed to assign the name in his <select>

<select class="form-control" id="exampleSelect1" name='exampleSelect1'>

Browser other questions tagged

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