Home > Back-end >  how can I view data using MySqlDataReader in MVC?
how can I view data using MySqlDataReader in MVC?

Time:03-01

In a Connection class.cs after entering my connection string, the Open.Connection and Close.Connection methods, I created the following method:

public void LibriList(Libro lista)
        {
            if (this.OpenConnection() == true)
                try
                {

                    string query = "SELECT * FROM libri_schema.libri";
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    MySqlDataReader rdr = cmd.ExecuteReader();
                    List<Libro> model = new List<Libro>();
                    while (rdr.Read())
                    {
                        var details = new Libro();
                        details.Titolo = rdr["Titolo"].ToString();
                        details.Autore = rdr["Autore"].ToString();
                        details.Editore = rdr["Editore"].ToString();
                        details.ISBN = rdr["ISBN"].ToString();
                        /* details.Prezzo = rdr["Prezzo"].ToString();
                         details.Pagine = rdr["Pagine"].ToString();
                         details.Quantità = rdr["Quantità"].ToString();*/
                        model.Add(details);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    this.CloseConnection();
                }

this is my controller:

[HttpGet]
public ActionResult Libri()
{
    return View();
}

//public ViewResult Registrationorm()
[HttpPost]
public ViewResult Libri(Libro Lista)
{
    if (ModelState.IsValid)
    {
        var connection = new Connection();
        connection.LibriList(Lista);
        return View("Libri");
    }
    else
    {

        ViewBag.ErrorMessage = "Nessun Libro disponibile";

    }
    return View();
}

This is a Model:

        public class Libro
        {
            public string Titolo { get; set; }
            //string Autore da cambiare in Lista
            public string Autore { get; set; }
            public string Editore { get; set; }
            public string ISBN { get; set; }
            public int Pagine { get; set; }
            public decimal Prezzo { get; set; }
            public int Quantità { get; set; }


       

In mySQL Workbench I created a table with some books, how can I now view it through a view?

CodePudding user response:

You have to return the data from the method so you can supply it to the view. For example, change the method's return type:

public List<Libro> LibriList(Libro lista)

And return the data:

List<Libro> model = new List<Libro>();
while (rdr.Read())
{
  //...
}
return model; // <--- here

You may also need to add more return statements or throw statements to your method, depending on the logic. For example, if this return is inside a condition then you'd need some default for when the condition is not met. What you do in those situations is up to you. You can return some default (like an empty list), return null, or perhaps throw an exception if that condition shouldn't happen.

Once you're returning from the method, supply that data to the view:

var connection = new Connection();
var model = connection.LibriList(Lista);
return View("Libri", model);

Then in your Libri view you would declare the model type at the top of the view:

@model List<Libro>

And can use the Model property of that type anywhere in the view. (Which is covered by MVC tutorials and examples.)


As an aside, this is an anti-pattern:

catch (Exception ex)
{
    throw ex;
}

The statement throw ex is stripping useful information from the exception, information that could help you diagnose and resolve errors. If you need to re-throw the original exception as-is, just use throw; by itself. But if the catch block isn't doing anything and just re-throws the original exception, get rid of the catch block entirely.

  • Related