views:

165

answers:

3

Hello,

What is wrong with this code. The code is finding the javascript and debug1 shown. If I remove the parameter p the code also founds mycontrol action and debug2 is shown.

View:

function method(p) {
    alert("debug1");
    $.post('../MyController/MyAction/' + p, function() {
        alert("debug2");
        $('#panel').empty().html('<img src="../Content/images/ajax-loader.gif" / >');
        $('#panel').load('../Controller/Index');
    });
}

Controller:

    public ActionResult MyAction(int p)
    {
       // Some code

        return null;
    }
A: 

That function is a callback and will get called regardless of success or failure. If you're calling a webservice, try including a success = true/false property as part of your result. That way you can do the following

`$.post('../MyController/MyAction/' + p, function(result) {
    if (!result.success) return;

    alert("debug2");
    $('#panel').empty().html('<img src="../Content/images/ajax-loader.gif" / >');
    $('#panel').load('../Controller/Index');
});`
David Archer
yeah that might be a good ide, but the problem is that the execution not steping into my action method. If I change the inparametername in MyAction to id instead of index that I actually use it works. Someopne who can explain?
karl
@mattias is correct, in your case then it is a routing issue
David Archer
+2  A: 

Seems to me like you have a problem with your routes. You can't change the name of your parameter if you don't change your routes. A route that would work for your scenario is:

routes.MapRoute("MyRoute",
            "MyController/MyAction/{p}",
            new { controller = "MyController", action = "MyAction", p = "" }
            );
Mattias Jakobsson
A: 

Just change it like this: $.post('../MyController/MyAction/p=' + p, function() {

Alexander Marinov