简体   繁体   中英

How to access specific data index in Returned Data Array from aJax Request

Using MVC Pattern, I am trying to find a way to either (1) process Class/Model-type List data into Arrays in MVC Controller for easy index retrieval in View (aJax Function) or (2) send the unprocessed queried data from the Db and process the aJax result into a JSON object or something for easy retrieval of the specific member in the result.

Is there a way to convert a Model-type List into a Collection.Generic.List or generic Array eg:

Model-Type List (queryList)

using (var context = new ModelNameDbContext())
{
   List<ModelName> queryList = context.ModelName
                                      .OrderByDescending(x => x.Sn)
                                      .Where(a => a.Stuff== "n1");
}

In the end, I am trying to iterate queryList which might have 30+ members, for instance, into a generic array double[] , eg:

double[] DataArray;

for (int i = 2; i < 32; i++)
{
    DataArray[i] = queryList[I];       // Having issue here, cannot implicitly convert type Models.ModelName to double
}

So that I can return DataArray to an MVC View through aJax Request to call specific records more easily using indexes in the View:

Controller (C#)

public JsonResult RetrieveAllData()
{
    double? nullCheck;
    List<ModelName> DataArray= new List<ModelName>();
    //double?[] DataArray;
            

    using (var context = new ModelDbContext())
    {
        nullCheck = context.vFarms
                .OrderByDescending(x => x.Sn).Where(a => a.NodeId == "n1").Where(h => h.SnsImeCo2Pd1 == h.SnsImeCo2Pd1)
                .Select(x => x.SnsImeCo2Pd1).FirstOrDefault();

        if (nullCheck is not null)               
        {
            List<ModelName> queryList = context.ModelName
                                                               .OrderByDescending(x => x.Sn)
                                                               .Where(a => a.NodeId == "n1")
                                                               .FirstOrDefault(x => x.Sn == context.vFarms.Max(p => p.Sn));

            METHOD A1.1 - Foreach to iterate
            //var index = 0;
            //foreach (vFarm item in queryList)
            //{
            //    DataArray[index] = item;
            //    index++;
            //}

            METHOD A1.2 - Iterating queried List<> to Generic Array for index parsing in Viewer (METHOD B8)
            //for (int i = 2; i < 34; i++)
            //{
            //    DataArray[i] = queryList[i];
            //}

            METHOD A2 - Passing raw queried List<> to View for parsing
            //DataArray.Add(queryList);
            return Json(DataArray);
        }
        else
        {
            return Json(0.0);
        }
    }
}

View (JavaScript) - aJax Setting & Request

$.ajax({
    url: '../CSV/RetrieveAllData/',
    dataType: "json",
    success: function (result) {

        METHOD B1
        //var data = result.d;
        //for (var i in data) {
        //    alert(JSON.stringify(data[i]));
        //    var y1 = data[i].Data1,
        //        y2 = data[i].Data2;
        //}

        METHOD B2
        //const yAxisArray = JSON.parse(result);

        METHOD B3
        //var yAxisArray = $.parseJSON(result);

        METHOD B4
        //var data = result.d;
        //for (var i in data) {
        //    if (localForInCount == 14) {
        //        var y1 = data[i].Data1;
        //    }
        //    else if (localForInCount == 15) {
        //        var y2 = data[i].Data2;
        //    }
        //    //var y1 = JSON.stringify(data[i].Data1),
        //    //    y2 = JSON.stringify(data[i].Data2);
        //    localForInCount++;
        //}
        //localForInCount = 0;

        METHOD B5
        //$.each(result, function (i, item) {
        var y1 = item.Data1,
            //    y2 = item.Data2;
            //});

        METHOD B6
        //var realArray = eval(JSON.stringify(result));
        //console.log(realArray);
        //var y1 = realArray[14],
        //    y2 = realArray[15];

        METHOD B7
        var parsed_data = JSON.parse(result);
        console.log(parsed_data.success);
        var y1 = parsed_data.SnsImeCo2Pd1,
            y2 = parsed_data.SnsImeCo2Pd2;

        METHOD B8
        //var y1 = result[0],
        //    y2 = result[1];
    }

Alternatively, suggestions for parsing a Model-type Array in Javascript is welcomed too, but I am having issues implementing solutions from here , here and here .

Snippet of aJax Query Result Format

[{"sn":1027,"nodeId":"n1","datapt1":0,"datapt2":0,"datapt3":null}]

Just not too sure how to retrieve a specific member value from the aJax result. I think you are asking about how to parse the json data using javascript in client. You get the json converted from an array, the json should look like "[{},{},...]" .You can easily use index to traverse it. eg

//javascript
jsonData = "[{"sn":1027,"nodeId":"n1","datapt1":0,"datapt2":0,"datapt3":null}]";
var dataYouwant = jsonData[0];

But make sure the json is in correct format. Additional " and \ could cause error when parse the json data in javascript. If this occures, give JSON.parse() a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM