簡體   English   中英

@ HTML.Drop Down列表並顯示數據綁定信息

[英]@HTML.Drop Down List and displaying databound information

我目前正在嘗試將一組缺少的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;

任何幫助將非常感激! 提前致謝。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM