meds
meds

Reputation: 22926

Getting link to an action?

I have an action in a MobileController called 'myaction' and I call it with javascript like so:

 <script type="text/javascript">
    function SubmitData() {

        $.ajax(
            {
                type: "POST",
                url: "http://localhost:1613/Mobile/myaction",
                data: "id to post",
                success:
                function (result) {
                    window.alert("SUCCESS");
                },
                error: function (req, status, error) {
                    window.alert("ERROR!");
                }
            }
            );
    }
</script>

Notice however the url is not using relative paths, I tried making it just ~/Mobile/myaction but that didn't work.

Any ideas how I can make it so the url being pointed to will work in all cases and not just if the domain is localhost:1613? Like if I uploaded it to mysite.com it would find the action at mysite.com/mobile/myaction.

Thanks for any help!

Upvotes: 0

Views: 63

Answers (4)

RollerCosta
RollerCosta

Reputation: 5186

 url: "@Url.Content("~/appName/Mobile/myaction/")"


Html::"/appName/Mobile/myaction/"
I suggest you to give your app Virtual path "/appName" doing this i'll allow you to avoid appName in url

Upvotes: 2

Sascha
Sascha

Reputation: 10347

You can use something like this:

new UrlHelper( HttpContext.Current.Request.RequestContext ).Action( "<action>", "<controller>", new { id = 1 } )

I'm using this exactly to pass values to a javascript function

Upvotes: 1

Valamas
Valamas

Reputation: 24729

Have you tried:

url: "@Url.Action(....)",

Upvotes: 3

Adeel
Adeel

Reputation: 19228

Replace the url by:

url: "/Mobile/myaction"

With ~ sign, it will only work with server controls/functions.

Upvotes: 2

Related Questions