MVC 应用程序,设置会话变量然后打开 asp.net 弹出窗口

发布于 2024-12-27 12:03:27 字数 1423 浏览 6 评论 0原文

我想知道是否有可能实现以下目标。

在 MVC 应用程序中 - 有一个链接,用于查询数据库中的某些值,将这些值设置为会话变量,然后打开一个弹出窗口(这是 MVC 应用程序中的 asp.net web 表单),

它基本上是为了允许我们运行 Crystal Reports,该链接将在会话变量中设置报告 ID,然后可以在 ASP.NET Web 表单中访问该变量。 我的困惑是单击链接然后打开弹出窗口时会话变量的设置。 可以吗?如果可以的话有链接或指针吗?

编辑: JavaScript

<script language="javascript" type="text/javascript">
   function flagInappropriate(postId) {

       var url = "/Home/FlagAsInappropriate/" + postId;

       $.post(url, function(data) {
       if (data) {
           alert("True")

           } else {
               // callback to show error/permission
           }
       });
   }

控制器

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {

        [AcceptVerbs("POST")]
        public bool FlagAsInappropriate(int id)
        {
            // check permission
            bool allow = true;

            // if allow then flag post
            if (allow)
            {
                // flag post

                return true;
            }
            else
            {
                return false;
            }
        }





        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

I'm wondering if it is possible to achieve the following.

Within an MVC application -
Have a link which queries a database for some values, sets those values as session variables and then opens a pop-up window(which is an asp.net webform within the MVC app)

It's basically to allow us to run Crystal Reports, the link would set the Report ID in a session variable which would then be accessible in the asp.net webform.
My confusion is the setting of the session variable on click of the link and then opening the popup.
Can it be done and if so any links or pointers?

Edit:
Javascript

<script language="javascript" type="text/javascript">
   function flagInappropriate(postId) {

       var url = "/Home/FlagAsInappropriate/" + postId;

       $.post(url, function(data) {
       if (data) {
           alert("True")

           } else {
               // callback to show error/permission
           }
       });
   }

Controller

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {

        [AcceptVerbs("POST")]
        public bool FlagAsInappropriate(int id)
        {
            // check permission
            bool allow = true;

            // if allow then flag post
            if (allow)
            {
                // flag post

                return true;
            }
            else
            {
                return false;
            }
        }





        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-01-03 12:03:27

这是可以做到的,是的。我已经实现了类似的目的来生成报告(主要是报告 URL 对用户隐藏,因此可以使用一次性令牌来实现一些跟踪和身份验证)。我的解决方案如下:

  1. 对应用程序中的 Web 方法执行 AJAX 调用以设置相关会话变量。
  2. 从 Web 方法返回一个值以指示它是否成功。
  3. 对于 AJAX 调用的“成功”事件处理程序,打开相关的 ASPX 页面以生成报告。

就这么简单。 :)

这里有一些示例代码,根据您修改后的问题附加点击事件并执行 AJAX 调用:

<a href="#" id="ajaxTest-1" class="flag">Click to test AJAX call</a>

<script type="text/javascript">
    $(document).ready(function () {
        $(".flag").click(function () {
            flagInappropriate($(this).attr("id").split("-")[1]);
        });
    });

    function flagInappropriate(postId) {
        var url = "/Home/FlagAsInappropriate/" + postId;

        alert(url);

        $.post(url, function (data) {
            if (data) {
                alert(data);

            } else {
                // callback to show error/permission
            }
        });
    } 
</script>

It can be done, yes. I've achieved something similar for the purposes of generating reports (predominantly so the report URL is hidden from the user and so some tracking and authentication could be achieved using once-off tokens). My solution was as follows:

  1. Perform an AJAX call to a Web Method in your application to set the relevant session variable(s).
  2. Return a value from the Web Method to indicate whether it was successful.
  3. For the "success" event handler of the AJAX call, open your relevant ASPX page to generate the report.

Simple as that. :)

Here's some sample code to attach the click event and do the AJAX call, based on your amended question:

<a href="#" id="ajaxTest-1" class="flag">Click to test AJAX call</a>

<script type="text/javascript">
    $(document).ready(function () {
        $(".flag").click(function () {
            flagInappropriate($(this).attr("id").split("-")[1]);
        });
    });

    function flagInappropriate(postId) {
        var url = "/Home/FlagAsInappropriate/" + postId;

        alert(url);

        $.post(url, function (data) {
            if (data) {
                alert(data);

            } else {
                // callback to show error/permission
            }
        });
    } 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文