I am using dynamic SQL to try to take all rows of a certain attribute from the database and set it as a variable in a script in my CSHTML page. From debugging and trial and error, the script is not working in the top layout header of my file where I connect to the database.
This is the top of my "ChooseWeek.cshtml" page.
@using Microsoft.AspNet.Identity;
@using WebMatrix.Data;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
var db = Database.Open("PayForPlay");
var selectedData = db.Query("SELECT WeekNumber FROM dbo.Answers");
}
This is a sample of my body of the HMTL page, it has this 12 times for 12 different weeks(It is the same exact snippet of code for each week only changing the week number and id's):
<li id="wk1">
<a href="/Home/WeeklyPredictions?field1=1">
<img id="w1p" src="http://mghelmets.com/ncaa%20historical/north-carolina-15a.gif" alt="Week1" width="200" height="150" />
<span class="text-content"><span id="wk1m">Week1</span></span>
</a>
</li>
At the very end of the body of my HTML, I have a working script that changes the text on a mouseover:
document.getElementById("wk1").addEventListener("mouseover", msg1);
function msg1() {
document.getElementById("wk1m").innerHTML = (selectedData); }
What my goal is, is it make every week that is contained in Answers to be hidden. So if selectedData contained week number 1, the entire would be hidden.
I tried putting this at the top of my script and when I did this, my HTML page just skipped the entire script
if (selectedData.Contains("1")) {
document.getElementById("wk1").style.display="none"; }
else if (!selectedData.Contains("1")) {
document.getElementById("wk1").addEventListener("mouseover", msg1);
function msg1() {
document.getElementById("wk1m").innerHTML = ('<b>Carolina vs NC:</b></br>Click here to submit your Predictions for Week 1');
}
}
Is this because selectedData is only available in the top header? If that's the case is there something that I can do that does the same thing but in the top header?
Thanks in advance for your help!
Model.SelectedDatainto controller side, then usevar selectedData = @Model.SelectedDataandindexOfmethod in JS for client-side checking.