开发者

T4MVC url.action in jquery /view

开发者 https://www.devze.com 2023-01-21 17:45 出处:网络
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\'v

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 $a开发者_JAVA技巧jax params object with no parameters inside the CancelLock() call. frustrating :(


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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号