在回发后显示带有MVC C#的弹出确认消息

使用MVC框架与C#编码。 视图以标准HTML代码编写。 一旦用户点击提交按钮,我需要一条确认消息“您的消息已被发送”

这是控制器:

public ActionResult Index(ContactViewModel contactVM){ if (!ModelState.IsValid) { string url = Request.UrlReferrer.AbsolutePath+ "#contact"; return View(); } else { var contact = new Contact { Name = contactVM.Name, Email = contactVM.Email, Subject = contactVM.Subject, Message = contactVM.Message }; new Email().Send(contact); return RedirectToAction("Index"); } 

这是视图:

   

请帮助。

而不是RedirectToAction() ,返回View

  new Email().Send(contact); ViewBag.Message = "Message Sent"; return View(); 

在视图中:

 @if(ViewBag.Message != null) {  } 

添加内部控制器

  ViewBag.IsEmailSent=true; public ActionResult Index(ContactViewModel contactVM){ if (!ModelState.IsValid) { string url = Request.UrlReferrer.AbsolutePath+ "#contact"; return View(); } else { var contact = new Contact { Name = contactVM.Name, Email = contactVM.Email, Subject = contactVM.Subject, Message = contactVM.Message }; new Email().Send(contact); ViewBag.IsEmailSent=true; return RedirectToAction("Index"); } 

Index.cshtml页面

  @if(ViewBag.IsEmailSent) {  } 

将结果(从RedirectToAction("Index") )更改为提供确认的视图。

如果您不想拥有主要是现有页面副本(如Index )的内容,则传递包含ShowConfirmation标志的对象,并在索引视图中有一些逻辑来显示确认该标志是否已设置。