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

Asked

Viewed 4,561 times

3

I am 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 this case it would be the parameter that the user would type). Then, when the user filled the textbox and clicked 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 besides the @url. Action being a GET method, I also can’t insert variables in it (at least not that I know):

<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?

  • You can send this data through Razor or JS Ajax, as to put parameters for Get methods, you can put yes, if we could never query to return only one entity per ID

1 answer

3


A simple Submit is enough for this scenario. I recommend using the html helper BeginForm 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 your view code to the above example is enough for the action ShowClients receive the expected value.

Note that the behavior of tag input of the kind submit is to do the post of the form in which it is inserted by default.

ASP.NET MVC, by convention, receives the values of the fields of a form since the parameters of the action who receives the post have the same names as the elements on the screen.

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

Note: You should use this form only if the page was displayed through the same controller to which you should send the data.

  • not better to use: @using (Html.Beginform("Suaactionx", "Seucontrollerx", Formmethod.Get) { // ... } ?

  • He would need to use it this way in case he is doing the post for an action from a controller other than the one that loaded the form. And also if he wanted to send the form with get instead of post, which is not the ideal scenario.

  • I disagree, because assuming that the user is loading this page through the same controller and not mentioning this in the answer may lead the user to use his Get method in the wrong way, as I mentioned, the user could use without restriction.

  • True, I will edit my reply to increase the clarity of the implementation.

  • 1

    Thank you Marcell, this is exactly what I needed, just reference the action and the correct controller and everything was right! I can’t thank you enough!!!

  • 1

    @Nugosunes please mark his answer as correct then, so the next users with the same question will already go to his answer ;) +1

Show 1 more comment

Browser other questions tagged

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