How do I display all the columns in my BD asp.net mvc

0

I wanted to know how to loop to print the columns of my grid with the values of the database, but it only showed 1 result even the database being huge, here is the code, thanks for the help

CONTROLLER:

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Mvc;
using TelerikMvcApp1.Models;


namespace TelerikMvcApp1.Controllers
{
    public partial class GridController : Controller
    {
        public ActionResult GetContacts()
          {
            SqlConnection conexao = new SqlConnection(@"meubancodedados");
            conexao.Open();

            string strQuerySelect = "SELECT * FROM PessoaFisica";
            SqlCommand cmdComandoSelect = new SqlCommand(strQuerySelect, conexao);
            SqlDataReader dados = cmdComandoSelect.ExecuteReader();

            while (dados.Read())
            {

            var contacts = new List<OrderViewModel>
                  {

                      new OrderViewModel {CompanyName = "Alabaster Almonds", ContactName = "Alex Allentown", Nome = dados["nome"].ToString()},
                  };
                return Json(contacts);
            }
                return GetContacts();
          }
    }
}

VIEW:

@model TelerikMvcApp1.Models.OrderViewModel
@using TelerikMvcApp1.Models

@(Html.Kendo().Grid<OrderViewModel>()
                .Name("ExampleGrid")
                .Columns(columns =>
                {
                    columns.Bound(c => c.ContactName).Width(140);
                    columns.Bound(c => c.CompanyName);
                    columns.Bound(c => c.Nome);
                })
                .DataSource(s => s.Ajax().Read(r => r.Action("GetContacts", "Example")))
)
<script>

    $.ajax({
        type: "POST",
        url: "Grid/GetContacts",
        dataType: "json",
        data:{data:'B'},
        success: function (data) {
            $("#ExampleGrid").data("kendoGrid").dataSource.data(data);
        },
    });

</script>
    
asked by anonymous 09.05.2018 / 19:03

1 answer

3

This happens because you create a list at each loop and try to return this list still within the loop . This will cause the stream to still end in the first loop while .

The correct thing would be to add an item to an existing list every loop and only then return all links .

var contacts = new List<OrderViewModel>();

while (dados.Read())
{   
     contacts.Add(new OrderViewModel 
     {
         CompanyName = "Alabaster Almonds", 
         ContactName = "Alex Allentown", 
         Nome = dados["nome"].ToString()
     });            
}

return Json(contacts);
    
09.05.2018 / 20:57