【问题标题】:How to show the Error message when there is nodata?没有数据时如何显示错误消息?
【发布时间】:2013-12-11 07:40:40
【问题描述】:

我正在使用 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();
    }

我需要在 if 条件中给出什么代码来显示错误消息框?

【问题讨论】:

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


    【解决方案1】:

    像这样创建一个_MessageBox 部分(我在这里使用引导警报):

     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");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多