如何将警报或通知消息从控制器传递到View?

时间:2019-08-17 09:19:51

标签: c# asp.net-mvc-5 alert

我正在研究ASP.NET MVC 5项目。我试图将成功和失败作为定义明确的框从控制器发送到我的视图。我的问题可能重复,但是没有一个答案对我有用。

我尝试的主要代码:

public ActionResult Pay_EditSave()
{
     if(condition = true)
     {
          TempData["alertMessage"] = "Duplicate Record-Record already Exists!!!";
          return View();
     }
}

查看:

<div class="box-body">
@using (Html.BeginForm("Pay_EditSave", "Master", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
     if (TempData["alertMessage"] != null)
     {
         <div class="alert alert-danger alert-dismissable">
             <button type="button" class="close" data-dismiss="alert" aria-hidden="true"> × </button>
             @TempData["alertMessage"].ToString()
         </div>
     }
}
</div>

除此之外,我尝试了其他许多viewbag / tempdata /返回新的JavascriptResult /返回内容答案。这些都不适合我。在控制台调试器中,代码显示“ 500内部错误”。

重要提示:我在此处显示的代码也在“登录”页面中使用。在该页面上,它工作正常。我不知道为什么在其他页面上这对我不起作用。我在做什么错了?

请帮助。

2 个答案:

答案 0 :(得分:1)

您可以使用ViewBag或ViewData代替TempData。另一种方法是;您也可以使用Session。 如果这不能解决您的问题,请检查以下链接,希望您能找到解决方法。

Any reason why my ViewBag is not working?

MVC 5 asp.net, viewbag from controller to view is not working

答案 1 :(得分:0)

您可以使用ViewBag.obj1,ViewBag.obj2等。