2011-09-25 204 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();