下拉列表VS.提交/搜索HTML.Beginform

本文关键字:HTML Beginform 搜索 VS 提交 下拉列表 | 更新日期: 2023-09-27 18:10:41

总结:我正在构建一个应用程序功能来报告丢失的数据。缺少记录的不同名称填充在下拉列表中。然而,当我开始搜索该特定名称(或任何名称)的完整报告时,我有这个错误以及应用程序崩溃:

错误:NullReferenceException未被用户代码处理。
未设置为对象实例的对象引用。

My 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;
        }
    }

任何和所有的帮助将不胜感激!

下拉列表VS.提交/搜索HTML.Beginform

首先,在您的"Home"控制器中是否有一个名为"Index"的post action,该post action接收表单中的字段模型?

我会用DropDownListFor方法,而不是ViewData方法,而不是你做的方式。因此,在控制器中,使用从MissingQueryResults()获得的字符串列表创建SelectList

注意:代码没有经过测试,直接写在这里。

创建一个viewModel,其中包含您需要传递的所有数据和post到控制器动作:

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);
}

In your view:

@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)
}