【问题标题】:Generating correct url with Url.Action when Id is dynamic (client side)当 Id 是动态的(客户端)时,使用 Url.Action 生成正确的 url
【发布时间】:2011-11-16 13:26:44
【问题描述】:

从 JQGrid ,在双击时,我试图按照以下代码将用户导航到新页面

ondblClickRow : function (id) {
                location.href = '<% = Url.Action("Index","MyFeature") %>/' + id;
            }

我们的代码错误

得到的网址是

http://localhost:8080/MyFeature.mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a

在控制器之后添加索引可以解决问题,但是有没有更好的方法来做到这一点

location.href = '<% = Url.Action("Index","MyFeature") %>/Index/' + id;

http://localhost:8080/MyFeature.mvc/Index/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    您可以使用将在客户端上替换的占位符:

    ondblClickRow : function (id) {
        var url = '<%= Url.Action("Index", "MyFeature", new { id = "__id__" }) %>';
        window.location.href = url.replace('__id__', id);
    }
    

    【讨论】:

    • 哇,让我试试这个,我相信它应该可以工作并且给了我更多的想法。谢谢。我将在完成测试后立即更新答案
    • @ Darin - 它确实有效。我还尝试了一些以后可能不会导致问题的方法(不确定)。 ...... '' + id;.这也有效。
    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 2011-08-13
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2010-12-17
    • 2010-09-19
    • 2016-04-26
    相关资源
    最近更新 更多