Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Jquery数据表中发送错误消息警报_Javascript_Jquery_Asp.net Mvc_Exception_Datatable - Fatal编程技术网

Javascript 如何在Jquery数据表中发送错误消息警报

Javascript 如何在Jquery数据表中发送错误消息警报,javascript,jquery,asp.net-mvc,exception,datatable,Javascript,Jquery,Asp.net Mvc,Exception,Datatable,我使用的是mvc格式的JQuery数据表。使用条件绑定JQuery数据表。但我不知道如何在其他条件下显示错误消息。我需要显示错误警报消息服务器端是客户端。我的示例代码: public ActionResult Action(string EmpNo) { if (condition) { // data table passing } else

我使用的是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 
            }
         }

您可以返回JavascriptResult

像这样的东西

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

您应该返回
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响应中的自定义列名,您需要查看:


但我使用的是datatable,因此不可接受。我们在客户端使用jauery数据表,只需在datatable Format中发送错误即可
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...
    }
};