5

Hallo guys,

I'm using ASP.NET MVC with jquery and it's going great for now. Just, there is one question that is bothering me. How should I handle urls in jquery methods? I really wouldn't like to hard code it, like here:

 $(function() {  
        $.getJSON("/Home/List", function(data) {  
            var items = "---------------------";  
            $.each(data, function(i, country) {  
                items += "" + country.Text + "";  
            });  
            $("#Countries").html(items);  
        });  

       $("#Countries").change(function() {  
           $.getJSON("/Home/States/List/" + $("#Countries > option:selected").attr("value"), function(data) {  
               var items = "---------------------";  
               $.each(data, function(i, state) {  
                   items += "" + state.Text + "";  
               });  
               $("#States").html(items);  
           });  
       });  
   });  

It is highly recommended to use HTML helper methods to create links in MVC, like Html.ActionLink, Html.BeginForm so in case that someone change that HomeController is mapped on MyHome instead of Home there will be no problem.

So, how not to hard code the url like in example?

Also, I don't want to use ASP.NET Ajax because i agree with this answer asp-net-ajax-vs-jquery-in-asp-net-mvc.

Thanks

3 Answers 3

6

You could define multiple global javascript variables:

<script type="text/javascript">
    var listUrl = '<%= Url.Action("Index", "Home") %>';
    var statesListUrl = '<%= Url.Action("States", "Home") %>';
</script>

which will be used later by the $.getJSON methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe you should avoid global scope, better namespace them in (e.g.) ajaxUrlStack.myurl1 ...
1

A really simple and pragmatic approach I have been using, is to put something like this at the top of every master page:

<script type="text/javascript">
    var baseUrl = '<%= Url.Resolve("~") %>';
</script>

and then include all your javascript files afterwards, using baseUrl whenever it needs it.

1 Comment

Thanks, Mookid for your reply. This is indeed good advice which I need to apply. But what is more important to me is the how not to hard code url itself, Home/Index; Product/ShowAll etc...
1

I often only need the current controller and action in js. Thats why I included this in my MasterPage.

<script type="text/javascript">
  var controller = '';
  var action = '';
  controller =   '<%= ViewContext.RouteData.GetRequiredString("controller")%>' ;
  action =   '<%= ViewContext.RouteData.GetRequiredString("action")%>' ;
    </script>

1 Comment

thats a great technique, been trying to find a way to get the controller name from jquery

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.