【发布时间】:2013-04-23 03:20:51
【问题描述】:
我目前正在尝试将一组缺失的 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;
任何帮助将不胜感激!提前致谢。
【问题讨论】:
-
如果不知道
HomeModel.MissingQueryResults长什么样子,这个问题是不可能回答的。在更一般的说明中,MissingChemistryReports和MissingChem都应该在您的视图模型中,因为它们是您的视图的数据。您的视图模型不应该直接映射到数据库实体。 -
我向 Ant P 道歉,我现在将编辑问题。
-
您正在创建一个 DropDownList 对象,它可能是 Windows 窗体 DropDownList 或 asp.net DropDownList,而不是 MVC 助手。然后,您尝试将该 DropDownList 转换为 SelectList,但事实并非如此。您需要在控制器中创建一个 SelectList,而不是 DropDownList。
-
@Mystere Man,我可以将 H.MissingQueryResults 全部转换为 SelectList 吗?
-
做了更改,收到相同的错误:“没有类型为 'IEnumerable
' 的 ViewData 项具有键 'MissingChemistryReports'。”
标签: c# asp.net-mvc html razor asp.net-mvc-4