Pass parameters via post to Controller

1

I have a CSHTML where the fields will become parameters, which will be passed via post to an existing method in a controller. How do I do this? I'm searching, but I still have questions about how to pass, as the method will understand that it comes from a cshtml.

    
asked by anonymous 25.03.2014 / 12:52

1 answer

1

One of the ways to send parameter via post to your controller is by encapsulating its parameters within a form in CSHTML of type POST and sending them via submit.

You can use the helpers of MVC for this.

Example

In your Model:

 public class PessoaModel
{

    public string Nome {get;set;} 
    public string Senha {get;set;}
}

In your CSHTML View

@model PessoaModel

 @using (Html.BeginForm("Salvar","PessoaController", FormMethod.POST))
 {
    @Html.Label("Nome"):
    @Html.TextBoxFor(e => e.Nome) <br />
    @Html.Label("Senha"):
    @Html.TextBoxFor(e => e.Senha)
 }

On your Controller

public class PessoaController : Controller
{
    [HttpPost]
    public ActionResult Salvar (PessoaModel model)
    {
        //Seu codigo quando a requisição post acontecer
    }    
}

You can also send POST information through a form without the use of a model, using the helpers without the for termination, and having controller parameter of the same name passed in the helper parameter.

For example:

In your CSHTML:

@using (Html.BeginForm("Salvar","PessoaController", FormMethod.POST))
 {
    @Html.Label("Nome"):
    @Html.TextBox("Nome") <br />
    @Html.Label("Senha"):
    @Html.TextBox("Senha")
 }

On your Controller

public class PessoaController : Controller
{
    [HttpPost]
    public ActionResult Salvar(string nome, string senha)
    {
        //Seu codigo quando a requisição post acontecer
    }    
}
    
25.03.2014 / 13:10