C# 如何在MVC控制器中使用消息框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9646456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use message box in MVC controller?
提问by Neo
I've created a MVC application. 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?
我创建了一个 MVC 应用程序。用户必须注册,一旦完成,我会将他重定向到“谢谢”页面。但是,我只想向用户显示带有此消息的弹出窗口。我怎样才能做到这一点?
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();
}
采纳答案by Dhruv Rangunwala
@Reynolds
@雷诺兹
Your answer is perfect.
你的回答是完美的。
In Razor, the following line can be replaced
在 Razor 中,可以替换以下行
alert("@TempData[alertMessage]");
by the following
通过以下
alert('@TempData["alertMessage"]');
PS. Notice the quotes
附注。注意引号
回答by LiamB
On the web you will need to use Javascript to display a message box. The syntax (To go into your view is in it's simplest form)
在 Web 上,您需要使用 Javascript 来显示消息框。语法(以最简单的形式进入您的视图)
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.
这很简单,但应该给你一个概念。
回答by Reynolds
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. Hope this helps!
这将像通常为任何 JavaScript 编写脚本一样写出脚本。希望这可以帮助!
回答by Tony Ranieri
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...
如果您这样做,要记住的一件事是您不希望您的用户多次单击“提交”按钮,因此您可能希望在第一次单击后隐藏或禁用它,并在验证错误时显示/启用它。 ..
回答by vicky
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"])

