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?
– Renato Afonso
I want to know how to play in the empty <td></td> there. I tried upstairs @model List<Client> But it doesn’t work
– Danilo Assis