What’s wrong with that method?

Asked

Viewed 44 times

0

I’m 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 library class:

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 index view should be displaying the data of each table field that is 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 with this??

  • 1

    The method Getparcels is returning value? In Controller already resume the result direct to View? Already debugged the View, checking if the foreach is executed for the amounts that should come from the bank?

  • @Ricardopunctual I understood what was happening, I needed to add it here and solved the problem: parcelas.Add(parcela);

No answers

Browser other questions tagged

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