1

If I need to call some action method of a controller from within javascript code I can call it just passing the href, right? Something like that:

 $.colorbox({ href: '/Calendar/SessionPropertiesEditbox?starts='+start+' })

That's not gonna work. The problem is the exact link should include the domain name also. But you don't know what the domain name would be. It could be "http://localhost:7741" today, tomorrow could be absolutely different.

So how to emulate ActionLink behavior in javascript code?

2 Answers 2

3

You are incorrect; that will work.

It's a domain-relative path, so the browser will automatically add the current domain.

If your application is not running in the domain root, it will not work, since it will look in the domain root.
If so, you'll need to call Url.Action and pass its result to your Javascript.

In a Razor view, that would look like

<script>
    var url = "@Server.JavaScriptStringEncode(Url.Action(...))";
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

really... Oh... maybe my routing is broken.. I need to check that. Thanks
See my edit. Are you in the domain root, or are you in localhost:port/YourAppName?
no, no, no.. I can't include Razor syntax. I'm calling it from .js file. Or... I actually can(?)
You can put that in separate <script> tag in your view and use it to pass a parameter to the .js file.
Oh.. I figured that out. that's why it wasn't working. I have two similar Action methods one receives int and another string. And It does work if I delete one of them. Why can't I have two different Action methods with same name?
1

Try to use JsAction http://jsaction.codeplex.com

Comments

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.