How to display the GetAll data I performed in an HTML table? MVC C # HTML

2

I'm running a simple mvc site, by college. I have to make a crud. I created a getall method that lists all existing clients in a list.

How can I step the same for the view? I tried the line below but I can not use ..

@model List<cliente>
  

Model Client with metadata get all

public List<Cliente> getall()
    {
        string StringDeConexao = @"CONNECTION";
        List<Cliente> clientes = new List<Cliente>(); //instancia lista

        using (SqlConnection cn = new SqlConnection(StringDeConexao))
        {
            {
                cn.Open();

                SqlCommand cmd = new SqlCommand("Select * from cliente", cn);

                var dr = cmd.ExecuteReader();

                while (dr.Read()) 
                {
                    Cliente cliente = new Cliente();

                    cliente.ID = (int)dr["ID"];
                    cliente.Nome = (string)dr["Nome"];
                    cliente.Email = (string)dr["Email"];
                    cliente.Tel = (string)dr["Tel"];
                    cliente.Cidade = (string)dr["Cidade"];
                    cliente.Documento = (string)dr["Documento"];
                    cliente.Data = (DateTime)dr["Data"];

                    clientes.Add(cliente); 
                }
            }

            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                cn.Close();
            }
            return clientes;
        }
    }
  

Customer Controller

public ActionResult List(Cliente cliente)
    {
        cliente.getall();
        return View(cliente);
    }
  

View Customer

div class="table">
                <table>
                    <thead>
                        <tr>
                            <td>ID</td>
                            <td>Nome</td>
                            <td>E-mail</td>
                            <td>Tel</td>
                            <td>Cidade</td>
                            <td>Documento</td>
                            <td>Data</td>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>

            </div>

How to display list in view without entity framework?

    
asked by anonymous 22.11.2016 / 03:49

2 answers

2

Okay. So what you have to do is make a foreach and create one for each column:

@foreach (var clientes in Model) {
   <td>
    clientes.Nome
   </td>   
   <td>
    clientes.Email
   </td>   
......
}
    
22.11.2016 / 11:28
0

Hello, brow!

From what I saw in your code, you are returning to the view the same client you are receiving as a parameter in the ActionResult List.

Change to:

public ActionResult List(Cliente cliente)
{
    var clientes = cliente.getall();
    return View(clientes);
}

Making this change, if your view is typed with

@model SeuNameSpace.Cliente

You need to change to

@model IEnumerable<SeuNameSpace.Cliente>

Well yes, just read with a foreach in View:

<table>
   <tr>
      <td>Nome</td>
      <td>Email</td>
   </tr>
   <tr>
      @foreach(var cliente in Model)
      {
        <td>@cliente.Nome</td>
        <td>@cliente.Email</td>
      }
   </tr>
</table>
    
23.11.2016 / 20:08