有nodata时如何显示错误消息?

时间:2013-12-11 07:40:40

标签: c# asp.net-mvc asp.net-mvc-3 c#-4.0 messagebox

我正在使用Asp .net MVC3,以下是我的控制器方法,

      public ActionResult TravelReadyAdminDownloadInvoke(int intId, int intMonth)
    {
        TravelReadyAdminModel objTravelReadyAdminModel = new TravelReadyAdminModel();
        try
        {
            if (intId == 3)
            {
                objTravelReadyAdminModel = objTravelReadyAdminModel.GetTravelReadyAdminRawExport(intId, intMonth);
                if (objTravelReadyAdminModel.lstRawDataEntities == null)
                {

                }
                else
                {
                    objTravelReadyAdminModel.ExportTravelReadyAdmin("TRAdminRawData", objTravelReadyAdminModel.lstRawDataEntities);
                }
            }
        }
        catch (Exception ex)
        {
            ILogManager LogManager = new LogManager();
            var frame = new StackFrame(0);
            LogManager.CallLogging(frame, ex.Message, ex.StackTrace);
            return RedirectToAction("Error", "Common");
        }
        return View();
    }

如果条件显示错误消息框,我需要在内部给出什么代码?

1 个答案:

答案 0 :(得分:0)

像这样创建_MessageBox部分(我在这里使用bootstrap提醒):

 if (TempData.ContainsKey("warning"))
 {
     <div class="alert alert-danger">
         <button type="button" class="close" data-dismiss="alert">x</button>
         @TempData["warning"]
     </div>
 }

在你的布局文件夹中添加以下内容:

@Html.Partial("_MessageBox")

在你的控制器中添加它(我更喜欢在我可以重复使用的基本控制器中调用它):

protected void Alert(string message)
{
            // Check to see if it exists already and removes it
    if (TempData.ContainsKey("warning"))
        TempData.Remove("warning");
    TempData.Add("warning", message);

}

然后在你的控制器中调用它:

catch (Exception ex)
{
    ILogManager LogManager = new LogManager();
    var frame = new StackFrame(0);
    LogManager.CallLogging(frame, ex.Message, ex.StackTrace);
    Alert("Warning message");
}