How to perform a postback operation in ASP.NET MVC?

In mvc, the page does not receive the message back, as in asp.net, then how can we perform postback operations in asp.net mvc2. for ex, how to perform specific actions when someone selects a check box? Thanks at Advance

+5
source share
3 answers

The mechanism of the reverse model in WebForms is called HTTP POST. This is how user input is passed back to the server.

You can do it manually. Attach the JavaScript handler manually to the "onclick" flag and execute a POST request to some URL. There, this request will hit some controller action, where you do what you want. For example, update the model (check / uncheck the box) and return the same view from which the POST originated. Now the view will show a different state for the checkbox.

WebForms mechanisms do almost the same thing, although these things are distracted from you. With ASP.NET MVC you need to learn how to do it yourself (which is always good).

+6
source

Your MVC Action method on your controller is your PostBack handler.

Start with a simpler example; simple post of HTML form:

<form action="/MyController/MyAction" method="post">
  <input type="text" name="myName" />
  <input type="submit />
</form>

. , , :

public class MyController: Controller
{
    public ActionResult MyAction(string myName)
    {
        // Do something with myName
        return new ContentResult { Content = "Hello " + myName };
    }
}

, . Javascript (jQuery - ) , . , 'onclick()' event XHR - Javascript, , post ( jQuery) .

, , -, HTML, HTTP Javascript.

+3

MVC Razor:

if (Request.HttpMethod=="POST") {
}
0

All Articles