[英]MVC3 Ajax ActionLink to call javascript function when completed with TempData
I have been trying to get a notification to show when a user invokes an action in my controller. 我一直在尝试获取通知,以显示用户何时在控制器中调用动作。 This works fine for most of my actions, however I use an
Ajax.ActionLink
to delete my items and therefore the page isn't refreshed and the javascript function isn't called and therefore the notification doesn't show. 这对于我的大多数操作都可以正常工作,但是我使用
Ajax.ActionLink
删除了我的项目,因此该页面没有刷新,并且没有调用javascript函数,因此没有显示通知。
In my _Layout
page I have the notification system as shown below: 在
_Layout
页面上,我具有如下所示的通知系统:
<script type="text/javascript">
//Used to display and close the system notifications
$(document).ready(function notify() {
$("#NotificationBox").show("slide", { direction: "down" }, 1000);
if ($("#NotificationAutoHide").val() == "true") {
$("#NotificationBox").delay(5000).hide("slide", { direction: "down" }, 1000);
}
});
</script>
<div id="NotificationDiv">
@if (TempData["Notification"] != null)
{
@Html.Hidden("NotificationAutoHide", TempData["NotificationAutoHide"])
<div id="NotificationBox" class="@TempData["NotificationCSS"]" style="display: none">
@TempData["Notification"]
</div>
}
</div>
In my Index
view (which uses the layout page) I have the Ajax ActionLink
that calls my Delete
action in my controller, which also populates the required TempData
properties. 在我的
Index
视图(使用布局页面)中,我有Ajax ActionLink
,它在控制器中调用了Delete
操作,该操作还填充了必需的TempData
属性。
@Ajax.ActionLink("Delete", "Delete", "MyController",
new { id = item.UserID },
new AjaxOptions {
HttpMethod = "Delete",
OnBegin = "JSONDeleteFile_OnBegin",
OnComplete = "notify"
},
new { @class = "delete-link" })
I thought by adding the call to the notify
function from the OnComplete
options in the ActionLink
then everything would work, but unfortunatly this doesn't seem to work. 我以为可以通过从
ActionLink
的OnComplete
选项中将调用添加到notify
函数中,然后一切正常,但是不幸的是,这似乎不起作用。
I have seen some examples that suggest sending the TempData
back through a JsonResult
from my conntroller, however I'm not sure how to tie that into my existing code. 我见过一些例子,表明发送
TempData
通过回JsonResult
从我conntroller,但我不知道如何打那个到我现有的代码。 (my controller already returns a JsonResult object for other uses) (我的控制器已经返回了JsonResult对象用于其他用途)
Could anyone help me get the TempData
back from the Ajax call and execute my notification system? 谁能帮助我从Ajax调用中获取
TempData
并执行我的通知系统?
Thanks very much for your time. 非常感谢你花时间陪伴。
Since TempData is a Server Side Code, I don't think it's possible. 由于TempData是服务器端代码,因此我认为不可能。 You should return this information by JsonResult.
您应该通过JsonResult返回此信息。
example: 例:
public JsonResult Delete(int id) { //do stuff return Json(new {param1 = "what you need", param2 = "more info"}, JsonRequestBehavior.AllowGet); }
Make sure that your notify
function is publicly visible so that you can invoke it from the AJAX success callback: 确保您的
notify
函数是公开可见的,以便您可以从AJAX成功回调中调用它:
<script type="text/javascript">
function notify() {
$("#NotificationBox").show("slide", { direction: "down" }, 1000);
if ($("#NotificationAutoHide").val() == "true") {
$("#NotificationBox").delay(5000).hide("slide", { direction: "down" }, 1000);
}
}
$(document).ready(notify);
</script>
The way you have declared the notify
function makes it impossible to invoke this function from an outside scope: 声明
notify
函数的方式使得不可能从外部范围调用此函数:
$(document).ready(function notify() { ... });
I don't even know if this is valid javascript syntax. 我什至不知道这是否是有效的JavaScript语法。 Never seen someone using it.
从未见过有人使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.