简体   繁体   English

jquery / view中的T4MVC url.action

[英]T4MVC url.action in jquery /view

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. 如何在我的视图中使用T4MVC构建Url.Action(),这将允许我使用jQuery选择器。 I've been attempting the following (in my javascript) without success: 我一直在尝试以下(在我的javascript中)没有成功:

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!! 我知道当答案到来时,这将是一个'doh'时刻,但对于我的生活,我无法弄清楚这一点!

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. [编辑] - 我想补充一点,如果我省略MVC.FundProperty.CancelLock()id参数并试图通过$ ajax调用发送params对象,那么编译器会抱怨调用中缺少的参数。 i can't therefore by-pass the javascript mish-mash by calling using the $ajax params object with no parameters inside the CancelLock() call. 因此,我不能通过在CancelLock()调用中调用$ ajax params对象而没有参数来绕过javascript mish-mash。 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. :) <%= ...%>块是纯服务器端代码,因此它不能使用JQuery选择器。 The best you can do with T4MVC might be something like: 使用T4MVC可以做的最好的事情可能是:

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. 您必须意识到在服务器上处理<%= ... %>而在调用函数cancelHoldBooking时在客户端上运行$("#propertyid").val()

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM