0

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!

3
  • In the javascript scope, selectedData will just be an undefined variable. This might be useful for you: stackoverflow.com/questions/14866539/… You seem to also have misunderstood how views work. If I understand you correctly, you want to get different kinds of data after the page has loaded, right? Logic in the views are only run once, then it's completely static. Use AJAX and a controller to fetch stuff dynamically. Commented Apr 22, 2017 at 22:07
  • 1
    On another note, it might be a good idea to keep these logic-thingys in the controller. Commented Apr 22, 2017 at 22:14
  • Better using a viewmodel and assign Model.SelectedData into controller side, then use var selectedData = @Model.SelectedData and indexOf method in JS for client-side checking. Commented Apr 25, 2017 at 0:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.