在控制器操作中返回任何类型的结果时调用 javascript 函数
我陷入了以下场景:
在我看来,有一个项目列表。根据项目代表的内容,单击项目将带您进入适当的视图并显示这些结果。现在在视图中显示结果不再是问题;工作正常。但是,某些项目应显示在弹出窗口中而不是视图中。我将部分视图与 bPopup 和 ShowListDialog(data) 等 JavaScript 函数一起使用来显示对话框并绑定数据。在返回操作中所需的任何结果(我怀疑 JavaScriptResult)时,如何调用 javascript 函数。
我认为我的操作目前应该是这样的:
public ActionResult ResponseItem(int reference) { ResponseBase 响应 =repository.RetrieveResponse(reference);
if (response.ShouldShowInView)
{
return View(response.RedirectUrl, response.Data);
}
else
{
return JavaScript("ShowListDialog(" + response.Data.JsonString + ");");
}
我一直在查看返回 JavaScript 结果的示例,但
我什至无法让它工作。如果我说这样的话:
return JavaScript("alert('Hello');");
然后在 IE 中它会询问我是否要打开 javascript 文件;在 Chrome 和 Firefox 中,它只显示alert('Hello');在页面上。
预先感谢您的帮助:) D
I'm stuck with the following scenario:
On my view there's a list of items. Depending on what the item represents, clicking on an item will take you to the appropriate view and display those results. Now displaying results in a view is not a problem; that is working fine. However, some items should be displayed in a popup and not in a view. I'm using a partial view together with bPopup and a javascript function like ShowListDialog(data) to show the dialog and bind the data. How do I call the javascript function upon returning whatever result needs be (I'm suspecting JavaScriptResult) in the action.
What I think my action should look like at the moment is:
public ActionResult ResponseItem(int reference)
{
ResponseBase response = repository.RetrieveResponse(reference);
if (response.ShouldShowInView)
{
return View(response.RedirectUrl, response.Data);
}
else
{
return JavaScript("ShowListDialog(" + response.Data.JsonString + ");");
}
}
I have been looking at examples of return a JavaScript result and I can't even get that to work. If I say something like:
return JavaScript("alert('Hello');");
then in IE it asks me if I would like to open the javascript file; and in Chrome and Firefox it simply displays alert('Hello'); on the page.
Thanks in advance for help :)
D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当循环浏览这些项目时,您可以决定哪个是弹出的,哪个不是,然后基于此链接到 2 个单独的操作?
JavaScriptResult 仅在您通过 ajax 调用操作时才有效,因此这可能就是它不起作用的原因(如果您不这样做)。您可以使用它来判断是否是 ajax 请求,然后返回适当的结果。
when looping through the items can you decide which is pop up and which isn't then link to 2 separate actions based on that?
JavaScriptResult only works if you call the action through ajax so that might be why it's not working (in case you aren't). you could prob use this to tell in action if it's an ajax request then return the appropriate result.