如何在C#中通过else语句触发弹出窗口

发布于 2024-12-10 13:48:52 字数 467 浏览 0 评论 0原文

您好,我正在使用 MVC3,并且处于 C# 环境中。我希望在满足 else 语句时有一个弹出显示。 代码如下所示:

if (validate.Count < 1) {
    db.TutorStudents.Add(tutorStudent);
    db.SaveChanges();
    return RedirectToAction("Index");
} else {
    // need some form of popup message here that does not redirect the user                         
}

我一直在搜索并找到了JQuery解决方案,但它们对点击结果起作用......相反,我希望弹出窗口在到达else语句时起作用。欢迎任何解决方案。

先感谢您

Hi i'm using MVC3 and i'm in the C# environment. I would like to have a popup show when an else statement is satisfied.
the code looks like this:

if (validate.Count < 1) {
    db.TutorStudents.Add(tutorStudent);
    db.SaveChanges();
    return RedirectToAction("Index");
} else {
    // need some form of popup message here that does not redirect the user                         
}

I've been searching and I found JQuery solutions, but they work on a click result right... rather, I want the popup to work when the else statement is reached. Any solution is welcome.

Thank you in advance

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

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

发布评论

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

评论(2

忆沫 2024-12-17 13:48:52

您无法直接从服务器在客户端显示弹出窗口。

但是您可以将一些标记值放入 ViewDataViewBag 并将该值传递给视图中的脚本:

操作中

        if (validate.Count < 1)
        {
            db.TutorStudents.Add(tutorStudent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            ViewBag.ShowMessage = true;
            //need some form of popup message here that does not redirect the user                            
        }

在视图中的

@if (ViewBag.ShowMessage)
{ 
    <script type="text/javascript">
        $(document).ready(function () {
            $(".message-box").show();
        });
    </script>
    <div class="message-box">Some Message here</div>
}

You can't show popup on the client side from the server directly.

But you can put some marker value to the ViewData or ViewBag and pass this value to the script in your view:

in the action

        if (validate.Count < 1)
        {
            db.TutorStudents.Add(tutorStudent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            ViewBag.ShowMessage = true;
            //need some form of popup message here that does not redirect the user                            
        }

in the view

@if (ViewBag.ShowMessage)
{ 
    <script type="text/javascript">
        $(document).ready(function () {
            $(".message-box").show();
        });
    </script>
    <div class="message-box">Some Message here</div>
}
梨涡 2024-12-17 13:48:52

尝试:

if (validate.Count < 1){
    db.TutorStudents.Add(tutorStudent);
    db.SaveChanges();
    return RedirectToAction("Index");
} else {
    ViewBag.PopUp = true; 
    return View(); // or any logic to show the last-view to user again                          
}

并在视图中:

@if(ViewBag.PopUp == true){
    <script type="text/javascript">
        alert("Your message here...");
    </script>
}

更新: 您无法通过 Web 应用程序中的 C# 向用户显示弹出窗口。因为 C# 在服务器上运行,所以您必须创建客户端代码才能在最终用户浏览器上运行。因此,您必须通过 C# 创建逻辑来控制 App 并生成 HTML(和/或 JavaScript、jQuery、CSS)以实现您的目的。

try:

if (validate.Count < 1){
    db.TutorStudents.Add(tutorStudent);
    db.SaveChanges();
    return RedirectToAction("Index");
} else {
    ViewBag.PopUp = true; 
    return View(); // or any logic to show the last-view to user again                          
}

and in view:

@if(ViewBag.PopUp == true){
    <script type="text/javascript">
        alert("Your message here...");
    </script>
}

UPDATE: You can't show a popup to user via C# in Web-Apps. Because C# runs at server, and you have to create a client-side code to run on end-users browser. So, you must create a logic via C# to control App and generate HTML (and/or JavaScript, jQuery, CSS) to achieve your purpose.

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