0

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;
    }
2
  • Check the "net" tab in firebug and you will probably find the problem. Also, how is your routes set up? Commented Mar 5, 2010 at 8:44
  • Can you post the code that invokes 'method', and the relevant section from your Global.asax.cs file - I'd suspect that the value of p is not an integer and the URL can not , therefore, be routed correctly. Also, start using a HTTP sniffer, such as Fiddler (A lugin for IE) to find out exactly what's going on behind the scenes. Commented Mar 5, 2010 at 9:07

3 Answers 3

2

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 = "" }
            );
Sign up to request clarification or add additional context in comments.

Comments

1

Just change it like this:

$.post('../MyController/MyAction/p=' + p, function() {

Comments

0

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');
});`

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.