Home > OS >  Display data from a SQL Server stored procedure in an ASP.NET MVC view
Display data from a SQL Server stored procedure in an ASP.NET MVC view

Time:01-25

The stored procedure that I have carried out in SQL Server returns the following information:

enter image description here

I show what I have worked on so far.

Model

public class TarjetasInformativas
{
    public string PrimerNombre { get; set; }
    public decimal PrimerMonto { get; set; }
}

Class in which I make my connection to the database

public class DatosTarjetasInformativas
{
    public List<TarjetasInformativas> RetornarNombres()
    {
        List<TarjetasInformativas> objTarjetas = new List<TarjetasInformativas>();

        using(SqlConnection sqlConnection = new SqlConnection("Data Source=HN123; Initial Catalog=DBTEST; Integrated Security=True"))
        {
            string query = "SP_TARJETASINFORMATIVAS";

            SqlCommand cmd = new SqlCommand(query, sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;

            sqlConnection.Open();

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    objTarjetas.Add(new TarjetasInformativas()
                    {
                        PrimerNombre = dr["ENTIDADES"].ToString(),
                        PrimerMonto = decimal.Parse(dr["MONTO"].ToString()),
                    });
                }
            }
        }

        return objTarjetas;
    }
}

Controller

public ActionResult ObtenerNombres()
{
    DatosTarjetasInformativas objDTTarjetas = new DatosTarjetasInformativas();

    List<TarjetasInformativas> objTarjetas = objDTTarjetas.RetornarNombres();

    return View(objTarjetas);
}

View

@model WebPlantillaOpexLTE.Models.TarjetasInformativas
<div >
    <!-- small box -->
    <div >
        <div >
            <h3>@Html.LabelFor(m => m.PrimerNombre)</h3>

            <p>@Html.LabelFor(m => m.PrimerMonto)</p>
        </div>
        <div >
            <i ></i>
        </div>
    </div>
</div>

Within my h3 and p tags of my view, I'm looking to get the information from my SQL Server stored procedure.

enter image description here

Through the Html.LabelFor helper I was able to get only the variables that I declared in my model, and I need to display the content of my SQL Server stored procedure inside the card.

In case of doubt, within my model where I relate to my stored procedure through a breakpoint, I have verified that it receives the information.

I am new to this platform, and I would like to know what I could do to solve my problem.

I thank you in advance for taking the time to pay attention to my question and for the help.

CodePudding user response:

So, there is a couple things wrong in your Razor view.

  1. @model should be of type List<WebPlantillaOpexLTE.Models.TarjetasInformativas> since this is the returning type of your SP function and the model returned to the view.
  2. To show the contents of a list, you should use an foreach loop to iterate over your model, something like the following (may contain syntax error):
@model List<WebPlantillaOpexLTE.Models.TarjetasInformativas>
<div >

            @foreach(var item in @Model){
                <!-- small box -->
                <div >                    
                        <div >
                            <h3>@item.PrimerNombre</h3>
                            <p>@item.PrimerMonto</p>
                        </div>
                        <div >
                            <i ></i>
                        </div>                    
                </div>
            }

</div>

Some reference: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-7.0

CodePudding user response:

I achieved what was required, but I made some changes to my controller and deleted my class where I made the connection to my database. I did it with Entity Framework.

I show my controller, in my model and view nothing changes except what was corrected in the first answer.

 public ActionResult ListarTarjeta()
    {

        List<TarjetasInformativas> listaTarjetas = new List<TarjetasInformativas>();

        using (db)
        {
            var listTarjetasInformativas = db.SP_TARJETASINFORMATIVAS().ToList();

            foreach(var item in listTarjetasInformativas)
            {
                var asignar = new TarjetasInformativas
                {
                    PrimerNombre = item.NOMBRE,
                    PrimerMonto = (decimal)item.MONTO
                };

                listaTarjetas.Add(asignar);
            }
        }

        return View(listaTarjetas);
    }
  • Related