How to show the Getall data I performed in an HTML table? MVC C# HTML

Asked

Viewed 222 times

2

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

How can I pass the same to the view? I tried the line below but I can’t use it..

@model List<cliente>

Model Client with get all method

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;
        }
    }

Controller Cliente

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

View Cliente

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 view list in view without Entity framework?

  • But why can’t you? It’s a mistake?

  • I want to know how to play in the empty <td></td> there. I tried upstairs @model List<Client> But it doesn’t work

2 answers

2


Ok. 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>   
......
}
  • But the line "@model List<Client>" does not work, this foreach is based on it is not?

  • Yes. But again, it has to be more detailed. "It doesn’t work" why? It makes an error? Normally I put in the "@"model a Ienumerable: "@"model Ienumerable<client>

  • That’s exactly the line I’m not getting right. I put "@" model List<Client> "@" model Ienumerable<Client> And it doesn’t work. This missing line.

  • I’m going to ask you one more time: It doesn’t work, why? What is the error that gives?

  • I was able to use this way: "@"model List<Projeto.Models.Cliente> Depois no for each: "@"foreach (Projeto.Models.Cliente cliente in Model), the error indicated that a client object was coming while waiting for a list of it.

0

Hello, Brow!

From what I saw in your code, you are returning to the view the same client you are receiving as 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

Need to change to

@model IEnumerable<SeuNameSpace.Cliente>

Ay yes, just do the reading with a foreach on 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>

Browser other questions tagged

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