【问题标题】:How to pass value in window.location?如何在 window.location 中传递值?
【发布时间】:2014-02-21 07:02:24
【问题描述】:

我正在尝试添加类似这样的 QueryString

var id = e.row.cells[0].innerHTML;
var subTypeId = e.row.cells[1].innerHTML;

window.location = "/Master/ReceivedStatus?Id=" + '@EncryptDecryptHelper.EncryptString(id)' + "&SubTypeID=" + '@EncryptDecryptHelper.EncryptString(subTypeId)';

@EncryptDecryptHelper.EncryptString 总是期待字符串值并且是一个静态类。

但它显示错误,因为当前上下文中不存在 id 和 subTypeId。是否可以传递这样的值,如果可以,那么如何传递?

请有人帮助我并告诉我如何传递这样的值。

【问题讨论】:

  • 您希望 EncryptDecryptHelper 何时工作?这似乎是服务器端 Razor 语法,它生成的 javascript 然后必须在浏览器中执行。
  • 我的代码在 View 中并在服务器端传递值,EncryptDecryptHelper 将在服务器端工作,我将在 Controller 中解密值

标签: c# javascript parameter-passing


【解决方案1】:

变量需要在引号之外;

window.location = '/Master/ReceivedStatus?Id=' + '@EncryptDecryptHelper.EncryptString(' + id + ')&SubTypeID=@EncryptDecryptHelper.EncryptString(' + subTypeId + ')';

【讨论】:

    【解决方案2】:

    您似乎正在尝试在字符串中使用变量 id 和 subTypeId。您必须转义字符串才能引用该变量。

    window.location = "/Master/ReceivedStatus?Id=" + '@EncryptDecryptHelper.EncryptString(' + id + ')' + "&SubTypeID=" + '@EncryptDecryptHelper.EncryptString(' + subTypeId + ')';
    

    【讨论】:

      【解决方案3】:

      我正在使用下面的代码,它对我有用。

      window.location = '@Url.Action("ReceivedStatus", "Master", new { Id =  '@EncryptDecryptHelper.EncryptString(id)', SubTypeID = '@EncryptDecryptHelper.EncryptString(subTypeId)'})';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-18
        • 2016-06-14
        • 1970-01-01
        • 2013-01-28
        • 2018-05-31
        • 2013-09-05
        • 2019-06-23
        • 1970-01-01
        相关资源
        最近更新 更多