返回视图在控制器中使用警报脚本后 [英] Returning View After using an an alert script within the controller

查看:140
本文介绍了返回视图在控制器中使用警报脚本后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个对C#代码中管理JavaScript感兴趣的问题,而不是讨论这是否是一个好的设计。

This is a question out of interest in managing JavaScript within the C# code, rather than discussion on whether this is a good design.

我开始尝试创建一个使用此答案在控制器内发出警报。我理解,在控制器中使用JS并不常见。

I started experimenting with creating an alert from within the controller using this answer. I understand, it's not usual practice to use JS within a controller.

如果我在控制器中创建警报,我该如何管理程序流然后返回视图。返回警报时会干扰显示视图的进度。

If I create an alert within a controller, how can I manage the program flow to then return the view. As returning the alert is interfering with the progress to display the view.

第一种方式暂停DoSomething中的代码:

The first way suspends the code in DoSomething:

public ActionResult DoSomething()
{
    // code to get User
    if(User.Role == someRole)
    {
        return Content("<script language='javascript' type='text/javascript'>alert('Merchant on Hold');</script>");
     }
    // More code
}

public ActionResult Dashboard()
{
    // Do things
}

第二种方式是暂停仪表板中的代码。

The second way suspends the code in the Dashboard.

public ActionResult DoSomething()
{
    // code to get User
    if(User.Role == someRole)
    {
        return RedirectToAction("Dashboard", "AppUser", new { message = 1 });
    }
    // More code
}

public ActionResult Dashboard(int? message)
{
    if(message == 1)
        return Content("<script language='javascript' type='text/javascript'>alert('Merchant on Hold');</script>");
    // Do things
}

我无法理解如何使用返回内容显示警报,因为这会阻止控制器内的返回,或者是否有办法在关闭时指示警报。

I can't work out how to display the alert with using return Content, as this then prevents the returns within the controller being reached, or if there is a way to direct the alert on close.

我'我正在寻找一种方法来获得对警报之后的程序流程的控制,因为它不会呈现视图,并且它返回的视图不需要参数。

I'm looking for a way to gain control of the program flow after the alert, as it stands, it won't render the view, and the view it's returning to, requires no parameters.

刷新页面会导致警报重新显示。

Refreshing the page will cause the alert to re display.

所以我想知道这是不是可行的方式来显示警报,或者是否有办法在C#中管理它。

So I'm wondering if this is a viable way at all to display an alert, or if there is a way to manage this within the C#.

推荐答案

你可以利用TempData如果你真的想让你的程序流动,但我脑子里想到的东西可以在不使用TempData的情况下工作:

You could utilize the TempData if you really want to make your program flow, but something came in my mind which will work without using TempData like this:

public ActionResult Dashboard(int? message)
{
    if(message == 1)
        return Content(@"<script language='javascript' type='text/javascript'>
                         alert('Merchant on Hold');
                         window.location.href='/AppUser/Dashboard?message=2'
                         </script>
                      ");
    // Do things
}

现在当下次行动将是被叫消息将具有值2,它将跳过显示警报并继续前进以执行您需要的其他操作。

Now when next time the action will be called message would have value 2 and it will skip displaying alert and will move forward to do other things you need.

我希望它能让您知道我想说的是什么。

I hope it give you idea what i am trying to say.

这篇关于返回视图在控制器中使用警报脚本后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆