【问题标题】:How to send the error message alert in Jquery DataTable如何在 Jquery DataTable 中发送错误消息警报
【发布时间】:2013-03-14 04:46:41
【问题描述】:

我正在使用 mvc 格式的 JQuery 数据表。绑定JQuery数据表,使用条件。但我不知道如何处理其他情况,显示错误消息。我需要显示错误警报消息服务器端是客户端。我的示例代码:

 public ActionResult Action(string EmpNo)
        {
            if (condition)
            {
               // data table passing
            }
            else
            {
                //here how to show the error message client side or server side 
            }
         }

【问题讨论】:

    标签: javascript jquery asp.net-mvc exception datatable


    【解决方案1】:

    您可以返回一个 JavascriptResult。 JavaScriptResult

    return Javascript("yourJS COde");
    

    类似的东西

    public ActionResult TestJavaScript() {
        string s = "$('#divResultText').html('JavaScript Passed');";
        return JavaScript(s);
    }
    

    【讨论】:

    • 但我使用的是数据表,所以不能接受
    • 我们在客户端使用 jauery 数据表,我们只需要在数据表格式中发送错误
    【解决方案2】:

    您应该返回 JsonResult 并带有与 DataTables 的预期返回签名匹配的任意对象(或真实对象,如果您创建了一个)。比如:

    var result = // do something, get a list of stuff etc
    
    return new JsonResult {
        // `Data` is the thing that turns into your json response
        Data = new {
            error = result.Success ? "" : result.Message,
            fieldErrors = new bool[0], // just to fake an empty array
            data = new bool[0],
            aaData = result.Success ? result.Items.Select(o => new {
                //use arbitrary column names if you've specified them in config with `aoColumns` and `mDataProp`, see comment below
                OrderID = o.ID,
                ChannelID = o.Partner,
                ReferenceKey = o.PartnerReferenceKey,
                o.CustomerEmail,
                Status = o.Status.ToString(),
                Value = o.Total,
                CreatedOn = o.CreatedOn.ToString("yyyy-MM-dd HH:mm:ss"), // provide in interpretable format
            }) : (object) new bool[0], // must return an empty list in order for it to understand errors
            iTotalRecords = result.TotalCount,
            iTotalDisplayRecords = result.TotalCount, // should be different if filtering
            sEcho // this is provided by the request, not sure what it means...
        }
    };
    

    对于来自 ajax 响应的自定义列名,您需要查看:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多