我想通過下拉列表選擇值並傳遞給SQL查詢,但我不知道如何才能進行下一步。如何將Dropdownlist值傳遞給SQL查詢?
查看:
@{
List<SelectListItem> List = new List<SelectListItem>();
minList.Add(new SelectListItem {
Text=DateTime.Now.AddMinutes(-1).ToString(),
Value="1"
});
minList.Add(new SelectListItem
{
Text = DateTime.Now.AddMinutes(-2).ToString(),
Value = "2"
});
minList.Add(new SelectListItem
{
Text = DateTime.Now.AddMinutes(-3).ToString(),
Value = "3"
});}@Html.DropDownList("Min",List, "Select")
SQL語句(C#):
string sqlStr = "Select * From Data WHERE Time BETWEEN @A AND @B";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
DateTime now = DateTime.Now;
SelectListItem min = new SelectListItem();
cmd.Parameters.Add(new SqlParameter("@A", min.Value));
cmd.Parameters.Add(new SqlParameter("@B", now));
您需要將表單中選定選項的值發佈到調用查詢的控制器方法。 –