Can’t make it work Ajax with MVC 3

This is my Controller method [HttpPost] public ActionResult RemoveValue() return View(); And this is how i am trying to access to method@Ajax.ActionLink(“Asenkron process”, “RemoveValue”)But if i remove HttpPost attribute then it works.I have tested this on MVC 3 RC 2…………………………………….. The [HttpPost] …

via C Programming Language Tutorial » Search Results » ajax:

Can’t make it work Ajax with MVC 3

This is my Controller method
[HttpPost]
public ActionResult RemoveValue()

return View();

And this is how i am trying to access to method
@Ajax.ActionLink(“Asenkron process”, “RemoveValue”)But if i remove HttpPost attribute then it works.
I have tested this on MVC 3 RC 2
……………………………………..

The [HttpPost] attribute means that that method can only be reached via a POST request. If you were to look at the request in Firebug or something similar, you would see the request being made, and get a 500 error in response.
If you want the link to perform a POST, change it to:
@Ajax.ActionLink(“Asenkron process”, “RemoveValue”, new AjaxOptions HttpMethod=”POST”)EDIT
Upon further reading of the docs, Ajax.ActionLink does nothing with the result it gets unless you specify something for it to do. It also does a POST by default it seems, so the AjaxOptions isn’t needed. If you want to display the contents of whatever is in the RemoveValue view, you need to do something like the following:
@Ajax.ActionLink(“Text”,”RemoveValue”, new AjaxOptions UpdateTargetId=”resultDiv” )

The contents of resultDiv will be whatever is returned from the Action.
If you are looking for just some sort of confirmation:

@Ajax.ActionLink(“Text”,”RemoveValue”, new AjaxOptions OnSuccess=”onSuccess”, OnFailure=”onFailure” )This is, of course, assuming you want an asynchronous request. If you want a normal synchronous POST, where the browser goes to a new page, do something like this:
using(Html.BeginForm(“ActionName”,”ControllerName”,FormMethod.Post))


If you do this, the Action you POST to should end with return RedirectToAction(“AnotherAction”);. The browser will then make a GET request to AnotherAction. This prevents duplicate actions when the user clicks the back button and the “Data must be resubmitted to the page” popup in browsers.
Hopefully this is slightly more helpful.
……………………………………..

Maybe a Ajax.BeginForm around it?
@using (Ajax.BeginForm(new AjaxOptions UpdateTargetId=”resultDiv”, HttpMethod=”Get”))

For more info: Can’t make it work Ajax with MVC 3

C Programming Language Tutorial » Search Results » ajax

Can’t make it work Ajax with MVC 3

Share this post:

Related Posts

Leave a Comment