Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC JavaScript Routing

Tags:

asp.net-mvc

Spoiler alert: this is NOW a question, so apologies to anyone that read it purely as a discursive topic :)

Anyway, I was doing a little research today re adding routes via javascript when i thought that a bit of google research wouldn't hurt. Basically, my aim was to do away with the following type of construct within my views:

alt text

and replace it with something akin to:

alt text

well, i lucked out a little today after finding this fantastic article (which isn't mine nor do i have any affiliation other than respect for the piece of work):

http://weblogs.asp.net/zowens/archive/2010/12/20/asp-net-mvc-javascript-routing.aspx

this really has been a missing link (or so i thought) for me when dealing with routes via javascript. However, the 2nd code example is misleading and actually won't produce what the example leads on. Can anyone suggest a fix for this and/or an alternative solution to allow this fluent convention of js routes within mvc views??

cheers...

[edit] - question edited 22:16 GMT to explore deeper options on this topic, plus changed title (removed OT portion).

like image 977
jim tollan Avatar asked Jan 04 '11 20:01

jim tollan


1 Answers

So the question is why the second code example won't work as expected. Here's the answer, post currently doesn't return anything. This is an example of a certain developer not looking at the details of the code. When you use homePageUrl, the value will be undefined.

To actually get the home page URL, you'd do the following:

$.routeManager.action({controller:'Home', action:'Index'}).toUrl()

So, the moral of the story is that the code is a bit broken. The post action SHOULD return an object where you can put "toUrl()" right after the post is performed, like this:

$.routeManager.action({controller:'Home', action:'Index'})
              .post(function(data){ alert(data); })
              .toUrl();

I'll be fixing this bug in a bit!

like image 146
zowens Avatar answered Sep 22 '22 10:09

zowens