简体   繁体   English

如何在MVC控制器中使用消息框?

[英]How to use message box in MVC controller?

I've created a MVC application. 我已经创建了一个MVC应用程序。 User has to register and once it is completed, I'm redirecting him to "thank you" page. 用户必须注册,一旦完成,我将他重定向到“谢谢”页面。 However I would like just to show user a pop-up with this message. 但是,我想向用户显示此消息的弹出窗口。 How can I achieve this? 我怎样才能做到这一点?

My code: 我的代码:

[HttpPost]
public ActionResult Enquiry(Enquiry enquiry)
{
    if (ModelState.IsValid)
    {
        dbEntities.Enquiries.AddObject(enquiry);
        dbEntities.SaveChanges();
        enquiry.SendEnquiryEmail(enquiry);

        return RedirectToAction("Thankyou"); 
    }

    return View(enquiry);
}

//redirect to thankyou page
public ActionResult Thankyou()
{
    return View();
}

To ensure your 'alert' in your view only shows up when you intend (a redirect from your ThankYou method) and not when somebody accidentally navigates to your 'ThankYou' view 确保视图中的“警报”仅在您打算显示时(从您的ThankYou方法重定向)而不是在有人意外导航到您的“ThankYou”视图时

//redirect to thankyou page
public ActionResult Thankyou()
{
    TempData["alertMessage"] = "Whatever you want to alert the user with";
    return View();
}

Then in your "ThankYou" view, this: 然后在“ThankYou”视图中,这个:

   if(null != TempData["alertMessage"])
   {
      <script type="text/javascript">
       alert("@TempData[alertMessage]");
      </script>
   }

This will write out the script as you normally would for any JavaScript. 这将像往常一样为任何JavaScript写出脚本。 Hope this helps! 希望这可以帮助!

in controller use this code 在控制器中使用此代码

public ActionResult Edit(CoverLetterModel model)
{
    TempData["msg"] = "<script>alert('Change succesfully');</script>";
}

in view use this code 在视图中使用此代码

@Html.Raw(TempData["msg"])

@Reynolds @Reynolds

Your answer is perfect. 你的答案是完美的。

In Razor, the following line can be replaced 在Razor中,可以替换以下行

alert("@TempData[alertMessage]");

by the following 通过以下方式

alert('@TempData["alertMessage"]');

PS. PS。 Notice the quotes 注意引号

It sounds like you may want to display the "thank you" message box on the view where the user enters the registration data? 听起来您可能想在用户输入注册数据的视图上显示“谢谢”消息框?

If this is the case, you need to AJAX POST to an action, then handle the success/fail message that returns from the action in your client side javascript. 如果是这种情况,则需要对操作进行AJAX POST,然后处理从客户端javascript中的操作返回的成功/失败消息。

One thing to keep in mind if you do this is you don't want your users to click the "submit" button multiple times so you may want to hide or disable it after the first click and show/enable it on a validation error... 如果您这样做,请记住一件事是您不希望用户多次单击“提交”按钮,因此您可能希望在第一次单击后隐藏或禁用它并在验证错误上显示/启用它。 ..

On the web you will need to use Javascript to display a message box. 在网络上,您需要使用Javascript来显示消息框。 The syntax (To go into your view is in it's simplest form) 语法(进入你的视图是最简单的形式)

Alert("Hello There!");

You cannot call this directly from your controller. 您不能直接从控制器中调用它。 Simply put the above code into your ThankYou view. 只需将上面的代码放入您的ThankYou视图中即可。

This is very simply but should give you the concept. 这很简单,但应该给你这个概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM