How to send FORM values to the Controller in ASP.NET MVC?

2

I'm new to ASP.NET and am creating an MVC application. I have, in my controller a method that works as follows:

 public ActionResult ShowClients(string proc)
    {
        --(proc)EXECUTA ALGUMA LOGICA AQUI
       return View();

    }

The logic of this method has already been tested and is working perfectly, the problem is that I need to pass this string "proc" to this controller through a form that is in the view (in case it would be the parameter that the user would type). So when the user filled out the textbox and clicked on the submit button the method would be called with what was written in the textbox. With the code below, I was able to send the parameter to the method but the problem is that in addition to the @ url.Action being a GET method, I also can not insert variables in it (at least not that I know of):

<form method="post">
    <input type="text" id="NICK" name="proc" placeholder="Digite Nome ou CPF">
    <input type="submit" onclick="parent.location='@Url.Action("ShowClients", "Clientes", new { proc = "SAUL DOM" })';return false;" value="Pesquisar" >
</form>

Is there any way to do this?

    
asked by anonymous 13.07.2018 / 21:05

1 answer

2

A simple submit is enough for this scenario. I recommend using the html helper BeginForm of MVC for form generation:

@using(Html.BeginForm("ShowClients"))
{
    <input type="text" id="proc" name="proc" placeholder="Digite Nome ou CPF">
    <input type="submit" value="Pesquisar" >
}

Changing the code of your view for the above example is enough for the action ShowClients to receive the expected value.

Note that the behavior of tag input of type submit is to post of the form it is inserted by default.

ASP.NET MVC, by convention, receives field values from a form as long as the parameters of action that receives post have the same names as the elements on the screen.

The helper BeginForm accepts the name of the action to receive the submit form as one of its parameters.

Note: You should use this form only if the page has been viewed through the same controller to which you want to send the data.

    
13.07.2018 / 21:13