Home > OS >  Returning a result from a json query to Html tags using Id
Returning a result from a json query to Html tags using Id

Time:09-11

I will apologize in advance as my experience in javascript/Jquery/Json is very limited. Here is basically what I am trying to achieve. Put the results of the json in div tags. I have completed code that I used for graphs that works. I created these several years ago. I am trying to reuse some of this code for this area. I believe what I have is close, The Json result is perfect. However I need to put the results in div tags.

For purposes of length I will show two entries. It would just be rinse and repeat for the other sets.

Here is my Json:

    public JsonResult GetPickThree(int ball1, int ball2, int ball3)
    {
        
        var setOne = ball1 * 100   ball2 * 10   ball3;
        var setOneC = (from a in le.Pick3 where a.Picks == ball1 * 100   ball2 * 10   ball3 select a).Count();           

        return Json(new { result1 = setOne, result1a = setOneC }, JsonRequestBehavior.AllowGet);
    }

Here is the Code I have for the script:

 function getSetOne() {

            var ball1 = document.getElementById('ball1').innerHTML;
            var ball2 = document.getElementById('ball2').innerHTML;
            var ball3 = document.getElementById('ball3').innerHTML;
            var game = "Pick3";

            console.log("getSetOne")
            console.log(ball1, ball2, ball3)

            $.ajax({
                url: '/Numbers/'  game  '/'  ball1  '/'  ball2  '/'  ball3,
                data: JSON,
                contentType: "application/json; charset=utf-8",
                method: "post",
                dataType: "json",
                error: function (_, err) {
                    console.log(_, err)
                },
                success: function (response) {

                    console.log(response);
                    var jsonresult = response

                    var data1 = jsonresult.result1.map(function (e) {
                        return e.result1;
                    });

                    var data1a = jsonresult.result1.map(function (e) {
                        return e.result1a;
                    });
                    document.getElementById("SetOne").innerHTML = (data1).toLocalString();
                    document.getElementById("SetOneA").innerHTML = (data1a).toLocalString();
                }

            });

Then on the Html I have:

<div id="SetOne"></div><div id="SetOneA"></div>

It is very possible that I do not need to use the jsonresult.result1.map(function (e) and use something else in its place but I do not know what to use. The Json Result works as I can see it in my console window.

Thanks for your help, and please feel free to ask any questions or if you need to see any other code. I believe that I have put everything you will need.

Update: Added json result Set.

0 0 7
4:385 
{result: Array(14), result1: 7, result1a: 6 …}
result1: 7
result1a: 6
[[Prototype]]: Object

Update added server side query: I think this is what you want, if not let me know.

exec sp_executesql N'SELECT 
[Extent1].[Date] AS [Date], 
[Extent1].[Draw] AS [Draw], 
[Extent1].[Picks] AS [Picks]
FROM [dbo].[Pick3] AS [Extent1]
WHERE [Extent1].[Picks] IN (((@p__linq__0 * 100)   (@p__linq__1 * 10)   @p__linq__2),((@p__linq__3 * 100)   (@p__linq__4 * 10)   @p__linq__5),((@p__linq__6 * 100)   (@p__linq__7 * 10)   @p__linq__8),((@p__linq__9 * 100)   (@p__linq__10 * 10)   @p__linq__11),((@p__linq__12 * 100)   (@p__linq__13 * 10)   @p__linq__14),((@p__linq__15 * 100)   (@p__linq__16 * 10)   @p__linq__17))',N'@p__linq__0 int,@p__linq__1 int,@p__linq__2 int,@p__linq__3 int,@p__linq__4 int,@p__linq__5 int,@p__linq__6 int,@p__linq__7 int,@p__linq__8 int,@p__linq__9 int,@p__linq__10 int,@p__linq__11 int,@p__linq__12 int,@p__linq__13 int,@p__linq__14 int,@p__linq__15 int,@p__linq__16 int,@p__linq__17 int',@p__linq__0=8,@p__linq__1=1,@p__linq__2=5,@p__linq__3=1,@p__linq__4=8,@p__linq__5=5,@p__linq__6=5,@p__linq__7=1,@p__linq__8=8,@p__linq__9=8,@p__linq__10=5,@p__linq__11=1,@p__linq__12=1,@p__linq__13=5,@p__linq__14=8,@p__linq__15=5,@p__linq__16=1,@p__linq__17=8

And this for each result that it is looking for. I did not add this code to the question as I did not think it was relevant. But i am not sure what you want to see.

exec sp_executesql N'SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM [dbo].[Pick3] AS [Extent1]
    WHERE [Extent1].[Picks] = ((@p__linq__0 * 100)   (@p__linq__1 * 10)   @p__linq__2)
)  AS [GroupBy1]',N'@p__linq__0 int,@p__linq__1 int,@p__linq__2 int',@p__linq__0=8,@p__linq__1=1,@p__linq__2=5

CodePudding user response:

Try using:

var data1 = jsonresult.result1;

instead

  var data1 = jsonresult.result1.map(function (e) {
                    return e.result1;
                });

And for data1a:

var data1a = jsonresult.result1a;

instead

var data1 = jsonresult.result1.map(function (e) {
    return e.result1;
 });
                

Also you have typo at .toLocalString(); function. Replace it to toLocaleString();

  • Related