views:

64

answers:

1

My asp.net-mvc view has an included javascript file. This javascript file has the following line:

$("input[id='SearchBag.CompanyName']").autocomplete("Search.mvc/AutoComplete/");

This works. Great. But it would be even better if I could replace the autocomplete's path with a generated URL so if I change the routing paths, I won't have to alter any code in my JS files. In non-included files I can go like

$("input[id='SearchBag.CompanyName']").autocomplete('<%=Url.Action("AutoComplete", "Search") %>)

But in included files this will not get parsed. Any ideas?

+1  A: 

On the assumption you actually want to do something a little more sophisticated than you have posted...

You could create a "Javascript" controller whose views have text/javascript content.

The view content would mainly be literal javascript but it would allow you to use <%= %> helpers.

You would need to configure response caching appropriately to avoid excessive requests for this content.

However for simple literal URLs such as in your questions I'm not sure it would be worth the bother.

AnthonyWJones
I wonder if it would be handy to have a built in (ie. part of the framework) controller and/or ActionResult that generates the javascript necessary to build urls from templates and also includes your current route configuration?
Richard Szalay
Actually, it's just as simple as the example shows. I was hoping for a 3 minute fix now that could potentially save me several hours later. But with YAGNI in mind, I think creating a controller for that is probably a bit overkill indeed :) Thanks for the answer though.
borisCallens