I am creating an asp net mvc application, I created a domain class with:
public class Parcela
{
public int parcelaId { get; set; }
public decimal valor { get; set; }
public DateTime vencimento { get; set; }
public string status { get; set; }
public virtual int clienteId { get; set; }
public int numero { get; set; }
}
Then I created a method in my class library:
public class ParcelaBLL
{
//metodo abaixo para listar todos os clientes na tabela
public IEnumerable<Parcela> GetParcelas()
{
string connectionString = ConfigurationManager.ConnectionStrings["ConSQLServer"].ConnectionString;
List<Parcela> parcelas = new List<Parcela>();
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
var query = "Select parcelaId, clienteId, valor, vencimento, status, numero FROM Parcelas";
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Parcela parcela = new Parcela();
parcela.parcelaId = Convert.ToInt32(rdr["parcelaId"]);
parcela.clienteId = Convert.ToInt32(rdr["clienteId"]);
parcela.valor = Convert.ToDecimal(rdr["valor"]);
parcela.vencimento = Convert.ToDateTime(rdr["vencimento"]);
parcela.status = rdr["status"].ToString();
parcela.numero = Convert.ToInt32(rdr["numero"]);
}
}
return parcelas;
}
catch (Exception ex)
{
throw ex;
}
}
}
And here in my view index should be displaying the data of each field of the table that are saved in the database:
@model IEnumerable<BLL.Models.Parcela>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Adicionar nova parcela", "Create")
</p>
<table class="table" border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model.clienteId)
</th>
<th>
@Html.DisplayNameFor(model => model.numero)
</th>
<th>
@Html.DisplayNameFor(model => model.status)
</th>
<th>
@Html.DisplayNameFor(model => model.vencimento)
</th>
<th>
@Html.DisplayNameFor(model => model.valor)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
using (@Html.BeginForm("Deletar", "Parcelas", new { id = item.parcelaId }))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.valor)
</td>
<td>
@Html.DisplayFor(modelItem => item.vencimento)
</td>
<td>
@Html.DisplayFor(modelItem => item.status)
</td>
<td>
@Html.DisplayFor(modelItem => item.clienteId)
</td>
<td>
@Html.DisplayFor(modelItem => item.numero)
</td>
<td>
@Html.ActionLink("Editar", "Edit", new { id = item.parcelaId }) |
@Html.ActionLink("Detalhes", "Details", new { id = item.parcelaId }) |
@Html.ActionLink("Deletar", "Delete", new { id = item.parcelaId })
</td>
</tr>
}
}
</table>
Am I doing something wrong? Could someone help me in this ??