2011-09-25 201 views
0

錯誤:編譯錯誤

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 

代碼:

@{ 
    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> 

而且v1將代表一個數字或一個字母通過向列表進行排序。但我不斷收到這個錯誤。寫錯了什麼?也許一雙新鮮的眼睛會有所幫助:D

正如您在上面的代碼示例中看到的,變量Movies基於if/else語句。但是,如果/別似乎不喜歡推出變量,或者我只是寫錯了...

回答

0

答案很簡單,它不需要被包裝在一個if/else語句中該聲明適用於此查詢類型的兩種方式:

var v1 = Request["sort"]; 
var movies = db.Query(@"SELECT * FROM movies WHERE title LIKE '" + v1 + "%' ORDER BY title ASC").ToList();