Asp.net mvc ajax.BeginForm Redirecting

ajaxasp.net-ajaxasp.net-mvcasp.net-mvc-2

I dont think i quite get the Ajax functions in mvc, because i get this wierd problem.

I got the following code which makes my ajax call, it is placed in a partial view with a productList:

                <% using(Ajax.BeginForm("AddToBasket", "Basket", 
                            new { productID = item.Id }, 
                            new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "Basket", OnSuccess = "productAdded(" + item.Id + ")" })) { %>
                    <input type="image" src="/Content/addToCart.png" />
                <% } %>

I have a <div id="Basket"></div> on my masterpage

And this method in BasketController, which returns a partial view that is found in Basket/BasketList.ascx:

        [HttpPost]
    public ActionResult AddToBasket(int productID)
    {
        // DO STUFF

        return PartialView("BasketList");
    }

When i am logged in using the default asp.net membership it all works fine, it updates the basket and it is all async, but when i am logged out and is clicking the addToCart, it redirects me to Basket/AddToBasket?productID=1, which is a partial view.

Does anyone know why this happens?

I have a similar problem with an ajax.actionlink

<%= Ajax.ActionLink("Gem", "SaveBasket", "Basket", new AjaxOptions { HttpMethod = "Post" })%>

where it says "The resource cannot be found." when it should fire, which is placed in the BasketController

[HttpPost]
public void SaveBasket()
{
    // DO STUFF
}

Best Answer

It sounds like you have a javascript error somewhere that is blocking the AJAX stuff that should be happening. Can't say why that would only happen when logged out though.

Do you have any errors in the error console/firebug?

Related Topic