【问题标题】:Passing Model id on Ajax.Actionlink OnSuccess to another Javascript function将 Ajax.Actionlink OnSuccess 上的模型 id 传递给另一个 Javascript 函数
【发布时间】:2012-04-26 20:18:04
【问题描述】:

我似乎无法找出正确的语法。似乎什么都试过了:

@model User

...

@Ajax.ActionLink("Add", "AddUser",new {id = Model.Id }, new AjaxOptions
{
HttpMethod = "post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "dialog-add-user",
OnSuccess = **"function(){ AddUserShow(" + @Model.Id + ");}",**
OnFailure = "userFailure"
}

成功后,我只需将我的 userId 传递给另一个 Javascript 方法。似乎没有任何效果。我环顾四周,找到了一些建议,但仍然没有运气。 我试过了:

 "new Function('AddUserShow(" + @Model.Id + ")')",
 "function(){ AddUserShow(" + @Model.Id + ");}",
 "function(){ AddUserShow(this," + @Model.Id + ");}",

 Also tried sticking the user ID in a hidden field to try and pass it later from functions above instead of accessing @Model:
 "AddUserShow($('#Id').val()))",

接收端代码:

function AddUserShow(id)
{
 alert(id);
}

如果重要的话,正在传递的 ID 是一个 Guid。

有什么想法吗? 提前致谢

【问题讨论】:

    标签: c# javascript jquery ajax model-view-controller


    【解决方案1】:

    您需要在其中使用引号,否则 javascript 会尝试将您的 Id 解释为变量。

    "AddUserShow(" + @Model.Id + ");" // bad. becomes AddUserShow(123abc);
    "AddUserShow('" + @Model.Id + "');" // good! becomes AddUserShow('123abc');
    

    【讨论】:

    • OnSuccess = "function(){ AddUserShow(' " + @Model.Id + " ');}", "Microsoft JScript 运行时错误:预期标识符" :(
    • 你能准确分享一下输出是什么吗?
    • Model.Id 是一个 GUID。我将其添加到 op.
    • 您能否查看源代码或使用脚本调试器查看您的代码输出的实际 javascript?运行时错误发生在哪里?
    • 知道了。你是对的。 function(){} 不一定在前面。只需使用单引号直接调用 AddUserShow 即可。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2012-10-17
    • 2021-10-06
    相关资源
    最近更新 更多