ASP.NET功能带来不完整的数据

发布于 2025-02-14 02:10:45 字数 4392 浏览 0 评论 0原文

我正在使用我自己执行的JavaScript中的Ajax,问题在于它正在重复其最后三个字段中的数据。验证数据库中的查询,数据与Ajax带来的不同,我显示了我的功能:

在ASP.NET中,我具有以下功能:

[HttpGet]
    public List<Reportes> GetScrapReport(string fecha, string fechaend)
    {
        try {
            var fechaparametro = new SqlParameter("@fecha", fecha);
            var fechafinparametro = new SqlParameter("@fechafin", fechaend);
            var listareport = _context.Reportes.FromSqlRaw($"SELECT DISTINCT idscrap, fecha, modelo, elemento, nombre, numeroparte, cantidad FROM F_GetScrapReport (@fecha, @fechafin)", fechaparametro, fechafinparametro);
            return listareport.ToList();
        }
        catch 
        {
            return new List<Reportes>();
        }
    }

Reports模型包含以下结构:

public class Reportes
{
    [Key]
    public int Idscrap { get; set; }
    public DateTime fecha { get; set; }
    public string modelo { get; set; }
    public string elemento { get; set; }
    public string? nombre { get; set; }
    public string? numeroparte { get; set; }
    public int? cantidad { get; set; }
}

Ajax Javascript我组合在一起的函数如下:

function GetScraptime()
{
    var j = 0;
    var fecha = document.getElementById('scraptime');
    var fechafin = document.getElementById('scraptimetwo');
    console.log(fecha.value);
    console.log(fechafin.value);
    if (fecha.value == "" || fechafin.value == "") {
        console.log("Uno de los parametros esta vacio");
    }
    else
    {
           $.ajax({
        method: "GET",
        url: "Reportes/GetScrapReport",
        contentType: "aplication/json; Charset=utf-8",
        data: { 'fecha': fecha.value, 'fechaend': fechafin.value },
        async: true,
        success: function (result)
        {
                console.log(result.length);
                $("#tabletimescrap").html('');
                while (j < result.length) 
                {
                    $("#tabletimescrap").append("<tr>");
                    $("#tabletimescrap").append("<td>" + result[j].idscrap + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].fecha + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].modelo + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].elemento + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].nombre + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].numeroparte + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].cantidad + "</td>");
                    $("#tabletimescrap").append("</tr>");
                    j = j + 1;
                }
            console.log(result);
        }
    });
    }
}

我要进行的查询是从我执行的SQL函数中获得的,我将函数附加在SQL中,以及执行它的结果:

CREATE FUNCTION F_GetScrapReport (@fecha varchar(20), @fechafin varchar(20))
RETURNS TABLE
    AS RETURN 
    (   SELECT [Scrap].IDScrap
                  ,[fecha]
                  ,M.modelo
                  ,[elemento]
                  ,P.nombre
                  ,P.numeroparte
                  ,[cantidad]
              FROM [dbo].[Scrap] FULL OUTER JOIN Scraparte Sc ON dbo.[Scrap].IDScrap = Sc.IDScrap 
              JOIN Modelo M ON dbo.[Scrap].IDModelo = M.IDModelo LEFT JOIN Parte P ON Sc.IDParte = P.IDParte
              WHERE dbo.[Scrap].fecha >= convert(varchar,REPLACE(@fecha,'"','') , 23) AND dbo.[Scrap].fecha<= DATEADD(HOUR,23.9999,convert(varchar, REPLACE(@fechafin,'"',''), 23)))

最后我得到的内容在执行查询的数据库中,如下:

SELECT DISTINCT idscrap, fecha, modelo, elemento, nombre, numeroparte, cantidad FROM F_GetScrapReport('2022-07-06','2022-07-06')

”在此处输入图像描述

问题是,在视图中,我将获得以下内容结果, nombre name ), numeroparte numberpart )和 cantidad ( 中写的内容没有意义

数量)列重复其值,这与db

I am consuming an Ajax in JavaScript that I perform myself, the problem is that it is repeating the data in its last three fields; validate the query in the DB and the data is different from what the Ajax brings, I show my functions:

In asp.net I have the following function:

[HttpGet]
    public List<Reportes> GetScrapReport(string fecha, string fechaend)
    {
        try {
            var fechaparametro = new SqlParameter("@fecha", fecha);
            var fechafinparametro = new SqlParameter("@fechafin", fechaend);
            var listareport = _context.Reportes.FromSqlRaw(
quot;SELECT DISTINCT idscrap, fecha, modelo, elemento, nombre, numeroparte, cantidad FROM F_GetScrapReport (@fecha, @fechafin)", fechaparametro, fechafinparametro);
            return listareport.ToList();
        }
        catch 
        {
            return new List<Reportes>();
        }
    }

The Reportes model contains the following structure:

public class Reportes
{
    [Key]
    public int Idscrap { get; set; }
    public DateTime fecha { get; set; }
    public string modelo { get; set; }
    public string elemento { get; set; }
    public string? nombre { get; set; }
    public string? numeroparte { get; set; }
    public int? cantidad { get; set; }
}

The AJAX JavaScript function that I put together is as follows:

function GetScraptime()
{
    var j = 0;
    var fecha = document.getElementById('scraptime');
    var fechafin = document.getElementById('scraptimetwo');
    console.log(fecha.value);
    console.log(fechafin.value);
    if (fecha.value == "" || fechafin.value == "") {
        console.log("Uno de los parametros esta vacio");
    }
    else
    {
           $.ajax({
        method: "GET",
        url: "Reportes/GetScrapReport",
        contentType: "aplication/json; Charset=utf-8",
        data: { 'fecha': fecha.value, 'fechaend': fechafin.value },
        async: true,
        success: function (result)
        {
                console.log(result.length);
                $("#tabletimescrap").html('');
                while (j < result.length) 
                {
                    $("#tabletimescrap").append("<tr>");
                    $("#tabletimescrap").append("<td>" + result[j].idscrap + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].fecha + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].modelo + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].elemento + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].nombre + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].numeroparte + "</td>");
                    $("#tabletimescrap").append("<td>" + result[j].cantidad + "</td>");
                    $("#tabletimescrap").append("</tr>");
                    j = j + 1;
                }
            console.log(result);
        }
    });
    }
}

The query that I am making is obtained from an SQL function that I perform, I attach the function in SQL and the result of executing it:

CREATE FUNCTION F_GetScrapReport (@fecha varchar(20), @fechafin varchar(20))
RETURNS TABLE
    AS RETURN 
    (   SELECT [Scrap].IDScrap
                  ,[fecha]
                  ,M.modelo
                  ,[elemento]
                  ,P.nombre
                  ,P.numeroparte
                  ,[cantidad]
              FROM [dbo].[Scrap] FULL OUTER JOIN Scraparte Sc ON dbo.[Scrap].IDScrap = Sc.IDScrap 
              JOIN Modelo M ON dbo.[Scrap].IDModelo = M.IDModelo LEFT JOIN Parte P ON Sc.IDParte = P.IDParte
              WHERE dbo.[Scrap].fecha >= convert(varchar,REPLACE(@fecha,'"','') , 23) AND dbo.[Scrap].fecha<= DATEADD(HOUR,23.9999,convert(varchar, REPLACE(@fechafin,'"',''), 23)))

And finally what I get in the DB performing the query is the following:

SELECT DISTINCT idscrap, fecha, modelo, elemento, nombre, numeroparte, cantidad FROM F_GetScrapReport('2022-07-06','2022-07-06')

enter image description here

The problem is that in the view, I get the following results, the nombre(name), numeroparte(numberpart) and Cantidad(quantity) columns repeat their values, which does not make sense with what is written in the DB

enter image description here

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

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

发布评论

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

评论(1

终陌 2025-02-21 02:10:45

问题是在报告中 数据模型,我正在用idscrap [key] [key]指示该字段它是 table 的标识符,但是在我的查询中不应该有标识符,它是选择,带有几个 JOIN ,所以,我用它装饰的模型[无钥匙]指示我的查询的返回将不包含标识符,因此它显示了DB中显示的数据。

[Keyless]
    public class Reportes
    {
        public int  Idscrap { get; set; } 
        public DateTime  fecha { get; set; }
        public string modelo { get; set; }
        public string elemento { get; set; }
        public string? nombre { get; set; }
        public string? numeroparte { get; set; }
        public int?  cantidad { get; set; }
    }

The problem was in the Reportes data model, where I am decorating the Idscrap field with [Key] indicating that it is the identifier of the table, but in my query there should not be an identifier, it is a SELECT with several JOIN , so, the model I decorate it with [Keyless] indicating that the return of my query will not contain an identifier, thus it shows me the data as it appears in the DB.

[Keyless]
    public class Reportes
    {
        public int  Idscrap { get; set; } 
        public DateTime  fecha { get; set; }
        public string modelo { get; set; }
        public string elemento { get; set; }
        public string? nombre { get; set; }
        public string? numeroparte { get; set; }
        public int?  cantidad { get; set; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文