Trying to display a variable in a for loop inside of razor with C#/Linq

Home / Uncategorized / Trying to display a variable in a for loop inside of razor with C#/Linq

Question:
I am trying to loop through a set of numbers and display them on a scorecard. If I step through the code, the variable returns the correct values, I just can’t get them to display.
@foreach (var g in Model)
{
@Html.Raw(string.Format("

"))
int grossScore = 0;

@Html.Raw(string.Format("

{0}

", g.PlayerName))
for (int s = 0; s < 18; s++) { @Html.Raw(string.Format(" "));

int holescore = 0;
var bretak = @g.Scores.Take(1).Skip(s).FirstOrDefault();
var werwee = @g.Scores.Where(b => b.Hole == s + 1).Select(i => i.Score).FirstOrDefault();
//if (bretak.Score > 0)
//{
// holescore = bretak.Score;
//}

holescore = werwee;

Html.Raw(string.Format("{0}", holescore));
//@Html.ToString(holescore);
//Html.Raw(holescore);
@Html.Raw(string.Format("

" + @holescore + "

"));
grossScore = grossScore + holescore;

@Html.Raw(string.Format("

"));
}
@Html.Raw(string.Format("

{0}

", grossScore))

@Html.Raw(string.Format("

", g.GameId))
@Html.Raw(string.Format("

", g.GameId))
@Html.Raw("

")
}

Player name displays correctly and the grossscore variable calculates and displays correctly. I get the correct number of boxes from inside the loop but holescore won’t display. Sorry that it may look a little messy but I left some of the code in to show the some of the things I have tried.


Answer:
I think you are misunderstood html.Raw, it is used to decode string into html string, not for outputting , for output you simply type so instead of something like this
@Html.Raw(string.Format("

" + @holescore + "

"));

just use this

@holescore

I suggest you going through this to get a clearer picture as I assume you didn’t based on your code above.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *