0

我目前正在尝试将一组缺少的 SQL 查询报告发布到我的网页上的下拉选择中。

我相信我的模型是正确的,但是即使我的控制器具有适当的语法,它将任何列表识别为 @HTML.DropDown 的一部分也存在一些问题。因此,应用程序在呈现网页之前崩溃。我希望这是一个简单的解决方法。

这是我要调用的函数的控制器代码:

public ActionResult FindDatMissingQuery()
        {
            HomeModel H = new HomeModel();
            DropDownList ddl = new DropDownList();
            ddl.DataSource = H.MissingQueryResults();
            ddl.DataBind();
            ViewData["MissingChem"] = ddl;

            return View();
        }

我的观点

@using (Html.BeginForm("FindDatMissingQuery","Home")){
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>Missing Chemistry Report</legend>
    <br />

@Html.DropDownList("MissingChemistryReports", ViewData["MissingChem"] as SelectList)

</fieldset>

}

我的模型功能:

 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 bewteen Dateadd(day, -1, Current_Timestamp) and Current_Timestamp AND heatname LIKE '[a,b,c,d]%' Order by heatname'");// + heatName + "'");
            SqlCommand mainquery = new SqlCommand("SELECT analysisvalue.analysisid, heatname, analysistime, sampletype, grade, productid, element, value FROM dbo.AnalysisValue INNER JOIN dbo.ChemistryAnalysis ON dbo.AnalysisValue.AnalysisID = dbo.ChemistryAnalysis.AnalysisID Where heatname = '" + heatName + "' Order By analysisvalue.analysisid Asc, element");

            using (SqlConnection conn = new SqlConnection(strSQLconnection))
            {
                missingQuery.CommandTimeout = 20000;
                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;

任何帮助将非常感激!提前致谢。

4

1 回答 1

1

这就是我在示例中使用下拉列表的方式:

    @Html.DropDownList("ActiveErrors", ListHelpers.PartsActive())


public static List<SelectListItem> PartsActive()
{
    List<SelectListItem> list = new List<SelectListItem>();
    list.Add(new SelectListItem { Text = "Yes", Value = "Y" });
    list.Add(new SelectListItem { Text = "All", Value = "A" });
    list.Add(new SelectListItem { Text = "No", Value = "N" });
    return list;
}

或使用模型属性

控制器:

ViewBag.RunTypeID = new SelectList(db.RunType, "ID", "RunTypeName").OrderBy(l => l.Text);

看法:

<span class="rightContent">
        @Html.DropDownList("RunTypeID", String.Empty)
    </span>
于 2013-04-22T18:18:23.473 回答