views:

397

answers:

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
}
+1  A: 

Are you sure all your pages are including the Microsoft Ajax libraries? And in the correct order?

Jaco Pretorius
I have: <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script> <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>in masterpage
Frost
+3  A: 

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?


I would have left a comment but didn't have enough points.

Andrew Mcveigh
it was something completely different, a jQuery library that wasn't added an used onSucces
Frost
and i found it using firebug, so thx to both :)
Frost