Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Razor
@{
    ViewBag.Title = "Getdata";
    <script src="~/Scripts/jquery-2.1.3.min.js"></script>
    <script>

        $(document).ready(function () {
            $("#btn").click(function () {
                $("#text1").val("");

                $.get("/country/Create", {}, function (data) {
                    $.each(data, function (index, row) {
                        $("#tb1").append("<table><tbody><tr><td>" + row.ID + "</td><td>" + row.Name + "</td></tr></tbody></table>");
                    });
                });
            });
        });
    </script>
}


country:<input type="text" id="text1" />
<input type="button" value="Add" id="btn" />
Posted
Updated 17-Feb-15 8:18am
v4
Comments
phil.o 17-Feb-15 9:31am    
Not a question. Just a code dump without any explanation.
Please see CodeProject QA FAQ
[no name] 17-Feb-15 9:36am    
hi there,Because its belong to the firs page i have sent or first question
phil.o 17-Feb-15 9:54am    
Still, this post does not qualify as a valid question in itself. If you want to bring some more informations on your previous question, click the improve button in it and add informations there.
King Fisher 17-Feb-15 13:52pm    
not clear.update your question with some information
Sergey Alexandrovich Kryukov 17-Feb-15 16:15pm    
Missing?! You are not showing anything related to SQL at all, only HTML and JavaScript.
—SA

1 solution

One thing I marked here is that you are making tables for each data row returned. But I don't think you needed that. Ideally all the rows should be in a Table.

So, the below code...
JavaScript
$.get("/country/Create", {}, function (data) {
    $.each(data, function (index, row) {
        $("#tb1").append("<table><tbody><tr><td>" + row.ID + "</td><td>" + row.Name + "</td></tr></tbody></table>");
    });
});

should be like...
JavaScript
$.get("/country/Create", {}, function (data) {
    $("#tb1").append("<table><tbody>");

    $.each(data, function (index, row) {
        $("#tb1").append("<tr><td>" + row.ID + "</td><td>" + row.Name + "</td></tr>");
    });

    $("#tb1").append("</tbody></table>");
});

If you are having any other issues regarding the data, then please debug and see what is exactly happening.
 
Share this answer
 
v2
Comments
enhzflep 18-Feb-15 2:40am    
Quick question: is that an errant brace in your code block, in the .each loop?

I.e
should row.Name + "</td></tr><"
actually be row.Name + "</td></tr>"?
Thanks man. Fixed now. :)
enhzflep 18-Feb-15 2:46am    
No worries mate. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900