如何从 aJax 请求访问返回数据数组中的特定数据索引

发布于 2025-01-11 07:38:59 字数 4826 浏览 0 评论 0原文

使用 MVC 模式,我试图找到一种方法(1)将类/模型类型列表数据处理到 MVC 控制器中的数组中,以便在视图(aJax 函数)中轻松检索索引,或者(2)发送未处理的查询来自 Db 的数据并将 aJax 结果处理为 JSON 对象或其他内容,以便于检索结果中的特定成员。

有没有办法将 模型类型列表 转换为Collection.Generic.List 或通用数组 例如:

模型类型列表(queryList)

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

最后,我尝试迭代 queryList 这可能例如,有 30 多个成员,放入通用数组 double[] 中,例如:

double[] DataArray;

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

这样我就可以通过 aJax 请求将 DataArray 返回到 MVC 视图 code> 调用具体使用视图中的索引更轻松地记录

控制器 (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);
        }
    }
}

视图 (JavaScript) - aJax 设置和设置请求

$.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];
    }

另外,也欢迎在 Javascript 中解析模型类型数组的建议,但我在实现 此处此处此处

aJax 查询结果格式的片段

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

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 e.g.:

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[], e.g.:

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}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-18 07:38:59

只是不太确定如何从 aJax 结果中检索特定的成员值。我认为您是在问如何在客户端中使用 javascript 解析 json 数据。你得到从数组转换而来的json,json应该看起来像“[{},{},...]”。你可以轻松地使用索引来遍历它。例如,

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

但请确保 json 的格式正确。在 javascript 中解析 json 数据时,额外的 " 和 \ 可能会导致错误。如果发生这种情况,请尝试 JSON.parse()

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. e.g

//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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文