Form validation ASP.net mvc!

Asked

Viewed 414 times

1

I have a form that validates the fields when trying to send information, only I have a "novapagina" option where I would not like to validate the fields, this would be possible?

In the views

<div class="container droppedHover">
    <br/>

    <div class="relative">
        <img src="~/Content/images/Checkmark.fw.png" alt="logo" class="img-circle">
    </div>

    <br/>


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.ValidationSummary(true)


        <div class="row">
            <div class="span6">
                @Html.TextBoxFor(x => x.login, new { placeholder = "correo electrónico", @class = "form-control input-lg" })
                @Html.ValidationMessageFor(x => x.login)
                @Html.TextBoxFor(x => x.senha, new { placeholder = "password", @class = "form-control input-lg", type = "password" })
                @Html.ValidationMessageFor(x => x.senha)
            </div>
        </div>

        <br />

        <div class="row">
            <div class="span6">
                <button class="btn btn-lg btn-block btn-primary " type="submit" name="opcao" value="pesquisar" >Login...</button>
                <button class="btn btn-lg btn-block btn-warning " type="submit" name="opcao" value="novapagina" >¿Olvidó la contraseña?</button>
            </div>
        </div>




    if (TempData["msg"] != null)
    {
        <div class="alert alert-info alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <strong>@TempData["msg"]</strong>
        </div>

    }
    else if (TempData["erro"] != null)
    {
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <strong>@TempData["erro"]</strong>
        </div>
    }




}




</div>

No Controller

        [HttpPost]
        public ActionResult Index()
        {
            string opcao = Request["opcao"];

            if (opcao == "novapagina")
            {
              return RedirectToAction("index", "RecuperaSenha");

            }
            else
            {
                //aqui vai pesquisar o login é senha
                return View();
            }

        }
  • Do you have a form and depending on an option, you will or will not request authentication? You are controlling this option through two submits on the Razor page?

1 answer

1


Any control of the page that is of type "Submit", will go through the validations. What you can do is change your redirect button, to a href, and just leave each other as "Submit", redirecting by Razor. Try this way:

<div>
 <button class="btn btn-lg btn-block btn-primary " type="submit"
     name="opcao" value="pesquisar" >Login
 </button>

  <a href="@Url.Action("Index", "RecuperaSenha")" class="btn btn-lg btn-block btn-warning">
    <span>Nova Página</span>
  </a>
</div>

  • your reply was perfect, very grateful for the help.

  • Oops, I’m happy to help =)

Browser other questions tagged

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