2013-04-24 71 views
0

摘要:我構建了一個應用程序功能來報告缺失的數據。缺少記錄的不同名稱會填充我的下拉列表。但是,當我開始通過該特定名稱(或任何名稱)搜索完整報告時,出現此錯誤以及應用崩潰:DropDownList VS.在HTML.Beginform上提交/搜索

錯誤:NullReferenceException未被用戶代碼處理。
對象引用未設置爲對象的實例。

我的視圖(錯誤所在位置):

VIEW:

@using (Html.BeginForm("Index", "Home", "POST")) 
{ 
    <div class="searchField"> 
     <input type="text" class="search-query" name="heatSearch" placeholder="Search"> 
     <button class="btn btn-success" type="submit">Search</button> 
    </div> 
    <br /> 
    <select> 
    @foreach (var item in (ViewData["MissingChem"] as IEnumerable<string>)) 
    { 
     <option> @item </option> 
    } 

    </select> 
} 

控制器:

public ActionResult Index() 
     { 
      Session["InitialLoad"] = "Yes"; 
      HomeModel H = new HomeModel(); 
      ViewData["MissingChem"] = H.MissingQueryResults(); 
      return View(); 
     } 

型號:

public List<string> MissingQueryResults() 
     { 
      //HomeModel Tolerances = new HomeModel(); 
      List<String> nameList = new List<String>(); 

      SqlCommand missingQuery = new SqlCommand("SELECT heatname FROM dbo.chemistrytable WHERE heatname NOT IN (SELECT heatname FROM dbo.chemistrytable WHERE sampletype = 'AVE') AND analysistime between '01-01-2013' and Current_Timestamp AND heatname LIKE '[a,b,c,d]%' Order by heatname");// + heatName + "'"); 
      SqlCommand mainquery = new SqlCommand("SELECT analysisv 

alue.analysisid, heatname, analysistime, sampletype, grade, productid, element, value FROM dbo.AnalysisValue INNER JOIN dbo.ChemistryAnalysis ON dbo.AnalysisValue.AnalysisID = dbo.ChemistryAnalysis.AnalysisID Where heatname = '" + "' Order By analysisvalue.analysisid Asc, element"); 

     using (SqlConnection conn = new SqlConnection(strSQLconnection)) 
     { 
      missingQuery.CommandTimeout = 20000;//Dateadd(day, -1, Current_Timestamp) 
      conn.Open(); 
      missingQuery.Connection = new SqlConnection(strSQLconnection); 
      missingQuery.Connection.Open(); 

      using (var reader = missingQuery.ExecuteReader()) 
      { 
       int fieldCount = reader.FieldCount; 

       while (reader.Read()) 
       { 
        for (int i = 0; i < fieldCount; i++) 
        { 
         nameList.Add(reader[i].ToString().Trim()); 

        } 

       } 
      }return nameList; 
     } 
    } 

任何和所有幫助會不勝感激!

回答

0

首先,您是否在您的「Home」控制器中有一個名爲「Index」的發佈操作,該操作接收帶有表單中字段的模型?

我會用DropDownListFor方法,而不是ViewData方法,而不是像你這樣做。 因此,在您的控制器中,使用您從MissingQueryResults()獲取的字符串列表創建SelectList

注:代碼沒有經過測試,直接寫在這裏。

public class VMHome { 
public SelectList MissingQueryResults {get; set;} // to build the dropdownList 
public string heatSearch {get; set;} 
public string MissingItem {get; set;} //for posting selected value 

} 

控制器

public ActionResult Index() 
{ 
      Session["InitialLoad"] = "Yes"; 
      VMHome h = new VMHome(); 
      h.heatSearch = ""; 
      h.MissingQueryResults = new SelectList(H.MissingQueryResults()) 
      return View(h); 
} 
[HttpPost] 
public ActionResult Index(VMHome model) 
{ 
      //do something with your posted data 
      // may be adding search result to the viewmodel 
      return View(model); 
} 

您認爲:

型號 你需要傳遞的所有數據和控制器動作後創建一個視圖模型

@model VMHome 
@using (Html.BeginForm("Index", "Home", "POST")) 
{ 
    <div class="searchField"> 
     @Html.TextBoxFor(x => x.heatSearch, new {placeholder = "Search", class = "search-query"}) 
     <button class="btn btn-success" type="submit">Search</button> 
    </div> 
    <br /> 
    @Html.DropDownListFor(x => x.MissingItem, Model.MissingQueryResults) 
} 
+0

是的,我有一個採用當前的搜索條件。我不知道這會對我的問題有所幫助,但我猜是這樣! – user1414048 2013-04-24 23:03:53