How do I display all the columns of my BD Asp.net mvc

Asked

Viewed 96 times

0

wanted to know how to loop when printing the columns of my grid with the values of the database, but it displayed only 1 result even the database being huge, here 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>

1 answer

3


This happens because you create a list for each loop and try to return this list still inside the loop. This will cause the flow to end still in the first loop while.

The correct thing would be to add an item to an existing list for each loop and only return later of all ties.

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);
  • even so my view does not appear anything in the list, even the database having things to read, the loop still does not work.

  • 1

    @Marcelohenriquedosreis That’s why you did something wrong...

  • now that I saw that the view problem was something else, your code was right, vlwzao <3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.