What is the equivalent of if($_POST) on . Net (ASP.Net MVC)?

Asked

Viewed 468 times

7

I’m coding a page in Visual Studio, in ASP.Net MVC and I need to display one div only when there is a POST on this page.

This is a form that will make the POST on the page itself. When POST, I will display a send confirmation message.

What I have is more or less like this:

@using (Html.BeginForm())
{
    <fieldset>
        <legend class="hidden">Contato</legend>

        <div class="msg-success">
            <p>Sua mensagem foi enviada com sucesso.</p>
        </div>

        @Html.LabelFor(m => m.Name, "Nome*")
        @Html.TextBoxFor(m => m.Name, new { id = "Name" })

        @Html.LabelFor(m => m.Email, "E-mail*")
        @Html.TextBoxFor(m => m.Email, new { id = "Email" })


        <input type="submit" id="Send" name="Send" value="Enviar" />
    </fieldset>
}

I would like an equivalence to if($_POST) to display the div:

<div class="msg-success">
    <h3>Obrigado!</h3>
    <p>Sua mensagem foi enviada com sucesso.</p>
    <p>Aguarde, que logo entraremos em contato.</p>
    <span class="close">X</span>
</div>

2 answers

11


In ASP.Net MVC, there are the markings in the action which indicate whether they will receive Type requests GET or POST. Like for example this:

[HttpPost] // Pode-se utilizar também [HttpGet], para receber requisições do tipo GET
public ActionResult ActionTest(object user)
{
    // esse action receberá apenas requisições do tipo POST
    return View();
}

Or there is still a solution that is more the way you seem to look for (Mode PHP), that is so:

public ActionResult ActionTest(object user)
{
    if (Request.RequestType == "POST")
    {
        //faça algo se for POST
    }
    return View();
}

Where you can check the type of the request, this is no better form, the 1° is more suitable, but nothing prevents you from using it.

In the second form you can validate any type of request, for example: GET, POST, PUT, DELETE, HEAD, etc.

Note: I mentioned that the 1° demonstrated form is better because it is more used, simpler to understand when observing the code, and is present in most of the materials available on Asp.net MVC, but the 2 ways can be used, choosing the one that best suits your needs.

1

Complementing @Fernando’s response, there are some other equivalents, but that do not necessarily provide a form to be submitted via GET:

1. Request.Form

Request.Form any and all value coming of the request, no matter if it is POST or GET. It is the basis of Model Binder.

2. Request.InputStream

Contains the entire HTTP request. It can be read as follows:

var dadosHttp = new System.IO.StreamReader(Request.InputStream).ReadToEnd(); 

In time, there is still the Attribute AcceptVerbs which you can set for the Action if the Action accepted POST, GET, both, or still others, as PUT, DELETE, HEAD, PATCH, etc..:

[AcceptVerbs(HttpVerbs.Post)] // Apenas POST
public ActionResult ActionPost(object user)
{
    // esse action receberá apenas requisições do tipo POST
    return View();
}

[AcceptVerbs(HttpVerbs.Get)] // Apenas GET
public ActionResult ActionGet(object user)
{
    // esse action receberá apenas requisições do tipo GET
    return View();
}

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] // Ambos
public ActionResult ActionAmbos(object user)
{
    return View();
}
  • 1

    Complementing the complement, the Request.Inputstream, is useful for example when you want to handle the request manually, such as receiving file upload, where you receive and read all bytes of Request. And the Acceptverbs, is a very flexible way of restricting and accepting multiple arguments in the same method.

Browser other questions tagged

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