views:

36

answers:

2

Hi all,

this may or may not be possible (and could well be in the docs but i've just missed it).

How do i structure a Url.Action() inside my view using T4MVC that will allow me to use jQuery selectors. I've been attempting the following (in my javascript) without success:

function cancelHoldBooking() {
    var url = '<%= Url.Action(MVC.FundProperty.CancelLock($("#propertyid").val())) %>';
    // other code omitted for brevity
}

i am able to successfully do the following:

function cancelHoldBooking() {
    var url = '<%= Url.Action("CancelLock", "FundProperty") %>';
    url += "?id=" + $("#propertyid").val();
    // other code omitted for brevity - in this case   
    // **I could of course have used the**:
    // var params = {id: $('#propertyid').val()};
    // **object**
}

i know this will be a 'doh' moment when the answer arrives, but for the life of me, I can't figure this out!!

cheers...

[edit] - i would just add that if i omit the MVC.FundProperty.CancelLock() id paramater and attempt to just send the params object via the $ajax call, then the compiler complains about the missing parameter in the call. i can't therefore by-pass the javascript mish-mash by calling using the $ajax params object with no parameters inside the CancelLock() call. frustrating :(

+1  A: 

I think you're trying to mix client and server code in a way that just can't work. :) The <%= ... %> block is pure server side code, so it can't be using a JQuery selector. The best you can do with T4MVC might be something like:

function cancelHoldBooking() {
    var url = '<%= Url.Action(MVC.FundProperty.CancelLock()) %>';
    url += "?id=" + $("#propertyid").val();
}

It still saves you from literal strings for the action and controller name, but won't help you with the param.

David Ebbo
david - thanks for the reply. i had magically just added an edit to express an issue with this idea just before you posted :). i do of course take on board the comment re mixing server and client side code..
jim
btw - thanks for the T4MVC template, it's been 99% sucessful for me, barring this one hiccup with jquery parameters (and the requirement to populate the parameter(s) inside the strongly typed T4MVC 'method' ). i may have to revert to my previous way of working for this type of action (i.e. where a jquery selector determines the value of the parameters) - but that's bearable for now, until i discover an elegant fix...
jim
David - +1 for you too in recognition of the T4MVC concept, if not a definitive answer to this question :). as i theorised in the other answer, i could of course make all my parameters nullable types. but this would be bending the code to fit the tool. shame there isn't a '3rd' way!!
jim
+1  A: 

You have to realize that <%= ... %> is processed on the server while $("#propertyid").val() is run on the client when the function cancelHoldBooking is called.

One way to solve your problem is this:

function cancelHoldBooking() {
    var url = '<%= Url.Action(MVC.FundProperty.CancelLock(999)) %>'; // Provide a magic number
    url += url.replace('999', $("#propertyid").val());  // Replace the magic number
    // other code omitted for brevity - in this case i could 
    // of course have used the params {id: $('#propertyid').val()} object
}
sheikhomar
sheikhomar - yes, this is what i had been doing prior to my T4MVC adoption... it works fine but i'm looking for a solution to integrating my T4 code with my javascript paramters on the client side. thanks tho..
jim
jim - i've changed my answer slightly. I hope that helps :-)
sheikhomar
sheikhomar - yes, that's definately one way to go. the other way would be to have the id property as int? id - i.e. nullable. but neither 'feel' right. however, +1 for taking the time to work around this. i may end up sticking with the 'out the box' 2nd implementation as the call actually in this case has 3 parameters and not just the one as exemplified above. ;)
jim