Generate table in view through a query to the database. JSON, JQUERY, MVC, .NET

6

I am new to the forum and I have broken my head all day because I can not solve this. I would greatly appreciate your help.

I have a very simple application in c # .net. I need to generate a table from a json that (according to me) contains the information of a query to the database (SQL SERVER). The table should appear when clicking on another main table that is in the view; From there, he grabs the ID to consult the BD. I am trying to generate the table with jquery, the other table is generated automatically with the model (Entity Framework). I hope I have been specific, here I leave part of my code to see if you understand a little better the idea:

MODEL

public class ApCommentView
{
    [Key]
    public int id_comment { get; set; }
    public int id_app { get; set; }
    public string version_app { get; set; }
    public string comentario { get; set; }
}

MANAGER

public class ApCommentManager
{
    LogBDEntities1 bd = new LogBDEntities1();

    public List<app_comments> idSearch(int id_ap)
    {
        var result = from c in bd.app_comments where
                     c.id_app.Equals(id_ap)
                     select c;

        return result.ToList();
    }
}

CONTROLLER

[HttpPost]
    public JsonResult Search(int id)
    {
        ApCommentManager ACM = new ApCommentManager();
        var results = ACM.idSearch(id);
        return Json(results);
    }

VIEW

<script type="text/javascript">

$(document).ready(
    function BUSCARid() {
    var tr = $('#dataApp').find('tr');
    tr.bind('click', function (event) {
        var row = $(this).closest('tr');
        var id_app = row.find("td:eq(0)").html().trim();
       alert(id_app);
        $.ajax({
            type: "POST",
            url: '@Url.Action("Search","Home")',
            data: { id : id_app },
            success: function () {
                alert("OK");

        $("#resultado").html('');
        for (var i = 0; i < result.length; i++) {
        $("#resultado").append("<li> " + result[i].id_app + " " + result[i].version_app + " " + result[i].comentario + " </li>");
        //}
    },
    error: function (xhr) {
        //debugger;
        console.log(xhr.responseText);
        alert("Error has occurred..");
    }
        });
    });
});

PS: The view where I want to generate the table refers to the model where the main table comes from, I do not know if that affects something.

    
asked by mldrd_dmngz 28.08.2018 в 00:33
source

2 answers

2

The solution has been given to you in the answer above. I explain what happens.

In your controller you are returning the results, which are a list of app_comment objects, through a return function Json (). This will format the objects to a string of JSON format that your JS code is able to interpret, however, you have to tell it what variable name you will grant it if the request is correct.

success: function(results){
    //Aquí la variable results es la lista en formato json de tus objetos app_comment.
    //results[0] sería tu primer objeto de la lista.
    //TODO
}

But if instead of results, you call it wololo, you could use it anyway, it's as if at the moment of making the call of success you assign to the input variable to the function the value that returns the call to the controller. So this would be like this:

success: function(wololo){
    //Aquí la variable wololo es la lista en formato json de tus objetos app_comment.
    //wololo[0] sería tu primer objeto de la lista.
    //TODO
}

The Ajax call is not able to assign the return value of the call to the URL to a default variable, so we have to give it a name to work with. The same would be if it were an error. But like this:

success: function(wololo){
    //Aquí la variable wololo es la lista en formato json de tus objetos app_comment.
    //wololo[0] sería tu primer objeto de la lista.
    //TODO
},
error: function(wololo){
    //En cambio aquí la variable wololo es el objeto error que devuelve el ajax.
    alert(wololo); //Mostrará en un alert el error.
}
    
answered by 09.01.2019 в 16:43
0

In your ajax, add the response parameter in the success. Apparently everything is ok.

    $.ajax({
            type: "POST",
            url: '@Url.Action("Search","Home")',
            data: { id : id_app },
            success: function (result) {//aquí
                alert("OK");

        $("#resultado").html('');
        for (var i = 0; i < result.length; i++) {
        $("#resultado").append("<li> " + result[i].id_app + " " + result[i].version_app + " " + result[i].comentario + " </li>");
        }
    },
    
answered by 28.08.2018 в 17:48