0

I am using the JQuery Data Table in mvc format. Binding the JQuery data table, using condition. But i don't know how to else the condition , show the error message. I need to show the error alert message server side are client side. My sample Code:

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

2 回答 2

0

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

于 2013-08-16T17:37:54.457 回答
0

你可以返回一个 JavascriptResult。JavaScript结果

return Javascript("yourJS COde");

像这样的东西

public ActionResult TestJavaScript() {
    string s = "$('#divResultText').html('JavaScript Passed');";
    return JavaScript(s);
}
于 2013-03-14T05:04:05.247 回答