2013-01-05 141 views
1

我有以下代碼:剃刀代碼錯誤

@{ 
    var db = Database.Open("WebPageMovies") ; 
    var selectCommand = "SELECT * FROM Movies"; 
    var searchGenre = ""; 
    var searchTitle = ""; 
    var selectedData = ""; 

    // search by Genre 
    if(!Request.QueryString["searchGenre"].IsEmpty()) { 
     selectCommand = "SELECT * FROM Movies WHERE Genre = @0"; 
     searchGenre = Request.QueryString["searchGenre"]; 
    } 

    // searching by title (but any word or words that match a title will work). 
    if(!Request.QueryString["searchTitle"].IsEmpty()){ 
     selectCommand += " AND Title LIKE @1"; 
     searchTitle = "%"+ Request.QueryString["searchTitle"] + "%"; 

     } 
    // if both textboxes are empty, then the following is dispayed 
    if(searchGenre != null) 
    { 
      selectedData = db.Query(selectCommand, searchGenre,searchTitle); 
    } 
      else 
      { 
       selectedData = db.Query(selectCommand,searchTitle); 

      } 
    var grid = new WebGrid(source: selectedData, defaultSort: "Genre", rowsPerPage:3); 

} 

<!DOCTYPE html> 

<html lang="en"> 
    <head> 

    <style type="text/css"> 
    .grid { margin: 4px; border-collapse: collapse; width: 600px; } 
    .grid th, .grid td { border: 1px solid #C0C0C0; padding: 5px; } 
    .head { background-color: #E8E8E8; font-weight: bold; color: #FFF; } 
    .alt { background-color: #E8E8E8; color: #000; } 
    </style>  

    <meta charset="utf-8" /> 
    <title>Movies</title> 
</head> 
    </head> 
    <body> 
     <h1>Movies</h1> 
     <form method="get"> 
    <div> 
    <label for="searchGenre">Genre to look for:</label> 
    <!-- in order for the textbox to rememebr what search value was entered, we need to provide a value attribute with that search value in our HTML--> 
    <input type="text" value="@Request.QueryString["searchGenre"]" name="searchGenre" value="" /> 
    @*<input type="Submit" value="Search Genre" /><br/>*@ 
    (Leave blank to list all movies.)<br/> 
    </div> 
    <div> 
      <label for="SearchTitle">Movie title contains the following:</label> 
      <input type="text" name="searchTitle" value="@Request.QueryString["searchTitle"]" /> 
      <input type="Submit" value="Search Title" /><br/> 
    </div> 
</form> 
    <div> 
    @grid.GetHtml(

    tableStyle: "grid", 
    headerStyle: "head", 
    alternatingRowStyle: "alt", 
    columns: grid.Columns(
     grid.Column("Title"), 
     grid.Column("Genre"), 
     grid.Column("Year") 
    ) 
) 
    </div> 
    </body> 
</html> 

不要擔心它的長度,只專注於剃刀代碼。簡而言之,我有兩個文本框和一個按鈕。我想在用戶在「標題」或「流派」或兩個搜索框中輸入值時顯示錶格。不過,我收到以下錯誤:

clickhere

+1

我有要求,不要把數據庫調用視圖。您必須對應用程序進行分層這裏視圖做了太多事情。這是您應用程序中的設計缺陷。在控制器,模型和庫/數據層中分配一些責任。 – Imrul

+0

@Imrul,我正在關注來自ASP.NETwebsite的教程。儘管欣賞你的反饋。 – Stranger

回答

2

你錯誤與恰好是這個字符串變量的類型:

var selectedData = ""; 

請參閱你想分配查詢結果,db.Query返回IEnumerable<dynamic>

selectedData = db.Query(selectCommand, searchGenre,searchTitle); 

更改該變量:

IEnumerable<dynamic> selectedData;