tags:

views:

27

answers:

1

Hi, I have a weird issue. I loaded a page using "LoadMyPageController" and there is an ajax_submit_button1 which can be used submit the page.

Also there is another ajax_submit_button2 to print the page. This button submits the view model of the page as a whole to the "PrintController" which has a "PrintData" action.

Now when I hit the "ajax_submit_button2", my PrintController.PrintData is not invoked. Instead when I check my fiddler tool the request is made as

http://localhost:18484/LoadMyPage/PrintData?Length=14

which is an invalid URL.

I have contructed my ajax_submit_button2 in such a way that it should invoke

http://localhost:18484/Print/PrintData?Length=14

But I don't know why LoadMyPage controller is present in my URL.

Any thoughts or comments would be appreciated. By any chance does asp .net MVC decides that it will take a default controller on its own if it can't find the controller action for any reason.

The code is a kind of tightly coupled so can't post it. I want to know if anyone experienced a problem like this.

A: 

This has nothing to do with the routing on the server since the request being made by the client has the wrong controller in it. I suspect that your code generating the url for the submit button is not correct -- i.e., not specifying the controller to be used -- or that you have a form around the submit button that is actually being invoked instead of (or in addition to) the ajax code. Note that if your submit handler doesn't return false, the default form action will be invoked and the form submitted normally. If you do have a form, make sure that the url on it is correct and that your submit handler returns false.

   $('#printForm').submit( function() {
        $.ajax({
            url: $(this).attr('action'),
            ...
        });
        return false; // this is important!
   });
tvanfosson