Reputation: 431
In mvc the page is not get post back as in asp.net then how can we perform postback operations in asp.net mvc2. for ex how to perform particular action when someone selects a chech box? Thanks in Advance
Upvotes: 7
Views: 12619
Reputation: 73
you can Put this inside an MVC Razor page:
if (Request.HttpMethod=="POST") {
}
Upvotes: 0
Reputation: 6316
Your MVC Action method on your Controller somewhat is your 'PostBack' handler.
Start with a simpler example; a simple HTML form post:
<form action="/MyController/MyAction" method="post">
<input type="text" name="myName" />
<input type="submit />
</form>
Now in your controllers action you can get the posted values and perform your tasks. When done give the browser back what it needs:
public class MyController: Controller
{
public ActionResult MyAction(string myName)
{
// Do something with myName
return new ContentResult { Content = "Hello " + myName };
}
}
As for a checkbox, it is different. You need to learn Javascript (jQuery is the most used library to use with that) and post the action using that. For example you can wire up to the check box 'onclick()' event and perform an XHR - a browser specific Javascript operation, post (you can use jQuery for that too) to your controller.
So you need to start thinking differently than webforms abstractions and get involved with HTML, HTTP and Javascript.
Upvotes: 3
Reputation:
The mechanism behind the postback model in WebForms is called HTTP POST. This is how user input is communicated back to the server.
You can do it manually. Attach a JavaScript handler manually to the checkbox "onclick" event and perform 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 checkbox) and return the same view from which the POST originated. The view will now show the different state for the checkbox.
The WebForms mechanisms do pretty much the same, though these things are abstracted away from you. With ASP.NET MVC you'll need to learn how to do it on your own (which is always a good thing).
Upvotes: 7