Display data from a database on a razor page

older_coder

Member
Joined
Dec 4, 2022
Messages
5
Location
Bedford, UK
Programming Experience
10+
Hi all,

So I'm getting data from the database, which I've managed to do correctly, but I'm having trouble displaying it on a razor page. I have the @{ } brackets but the code inside it displays on the screen rather than the values.

How do I get the actual data to appear on the page, rather than the code? I can paste the code I'm using if that would help.

Thanks in advance.
 
Yes, please share the code.

Also, what is the file extension of the file that has the code?
 
Yes, please share the code.

Also, what is the file extension of the file that has the code?
I'll get the code and paste it here shortly - I'm getting the data in a .cs file which is a controller class (I know it should be a model class but want to get this working first). I think the problem is connected to the way I'm passing the data from the class file to the view file.
 
So, here's the code in the HomeController.cs file and this works, when I display "Holiday" it does have the right data:
C#:
        public Dictionary<string, string> getHolidaysView()
        {

            string qryString = "SELECT * FROM vwHolidays";
            SqlConnection db_connection = connectDB();
            SqlCommand qryCmd = new SqlCommand(qryString, db_connection);
            db_connection.Open();

            var Holiday = new Dictionary<string, string>();

            using (SqlDataReader qryReader = qryCmd.ExecuteReader())
            {

                while (qryReader.Read())
                {
                   
                    {
                        Holiday["location"] = qryReader["location"].ToString();
                        Holiday["from_date"] = qryReader["start_date"].ToString();
                        Holiday["to_date"] = qryReader["end_date"].ToString();
                        Holiday["details"] = qryReader["details"].ToString();
                        Holiday["image"] = qryReader["image"].ToString();
                    };

                }

                db_connection.Close();
            }


            return Holiday;
        }

And this is the latest try in the index.cshtml file:
HTML:
    <div>

        <h1>myHoliday</h1>
        @{

            var HolidayList = Html.Action("getHolidaysView", "Home").ToHtmlString();

            foreach (var holiday in HolidayList)
            {

                <span>holiday.Value</span>

            }

        }

    </div>


And this is the output:

Code:
myHoliday
holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value holiday.Value


my database has 1 record with those five fields, so why it is looping so many times is also a mystery.

Thanks in advance for any help anyone can give.
 
Last edited by a moderator:
In your Razor, try changing line 11 to:
HTML:
<span>@holiday.Value</span>
 
var HolidayList = Html.Action("getHolidaysView", "Home").ToHtmlString();
HolidayList isn't a Dictionary, it's a string

You can foreach it, because foreaching a string enumerates the chars, so holiday isn't a keyvaluepair that you can retrieve the Value of

--

Do it how it's supposed to be done, rather than acknowledging this is the incorrect way, and finding workarounds to every barrier thrown up by doing something incorrectly in the first place
 
HolidayList isn't a Dictionary, it's a string

You can foreach it, because foreaching a string enumerates the chars, so holiday isn't a keyvaluepair that you can retrieve the Value of

--

Do it how it's supposed to be done, rather than acknowledging this is the incorrect way, and finding workarounds to every barrier thrown up by doing something incorrectly in the first place
Thank you for you reply, but please bear in mind I'm new to this and don't yet know the "correct" way of doing this. Feel free to tell me what the correct way is, I'm keen to learn.
 
Back
Top Bottom