Error:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'movies' does not exist in the current context
Source Error:
Line 12: }
Line 13: <h1>Movies</h1>
Line 14: <p>There Are @movies.Count Movies.</p>
Line 15: <ul class="thumbnails gallery">
Line 16: @foreach (var movie in movies) {
Source File: c:\Users\Oval Office\Documents\My Web Sites\mynewdb\Movies\Browse.cshtml Line: 14
Code:
@{
var v1 = UrlData[0];
Page.Title = "Movies";
var db = Database.Open("mysql");
if (v1 == "" || v1 == null) {
var movies = db.Query(@"SELECT * FROM movies ORDER BY title ASC").ToList();
}else{
var movies = db.Query(@"SELECT * FROM movies WHERE title LIKE '@0%' ORDER BY title ASC", v1).ToList();
}
}
<h1>Movies</h1>
<p>There Are @movies.Count Movies.</p>
<ul class="thumbnails gallery">
@foreach (var movie in movies) {
<li class="gallery">
<a href="@Href("~/Movies/View", movie.id)">
<img alt="@movie.title (@movie.year)" title="@movie.title (@movie.year)" src="@Href("~/Photo/Thumbnail", movie.img)" class="thumbnail-no-border" />
<span class="below-image">@movie.title</span>
<span class="image-overlay"><strong>@movie.year</strong></span>
</a>
</li>
}
</ul>
And v1 will represent either a number or a letter to sort the listings by. But i keep getting this error. What is written wrong? Maybe a fresh pair of eyes will help 😀
As you see in my code example above the variable Movies is based on an if/else statement. But if/else doesn’t seem to like pushing out variables, or maybe i’m just writing it wrong…
the answer is simple, it didn’t need to be wrapped in an if/else statement as the statement works both ways with this query type: