如何创建一个带有包含用户id的下拉列表的登录页面



我已经创建了控制器,但无法将下拉列表中的选定值和文本框值与数据库(SQL(进行比较。所有id都在下拉列表中,并从数据库中检索。我应该如何匹配文本框写入值

以下是HTML代码

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
<tr>
<td>
Fruit:
</td>
<td>
@Html.DropDownListFor(m => m.FruitId, Model.Fruits, "Please select")
</td>
</tr>
<tr>
<td>
Quantity:
</td>
<td>
@Html.TextBoxFor(m=>m.Password)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
[HttpPost]
public ActionResult Index(FruitModel fruit)
{
fruit.Fruits = PopulateFruits();
var selectedItem = fruit.Fruits.Find(p => p.Value == fruit.FruitId.ToString());
return View(fruit);
}
private static List<SelectListItem> PopulateFruits()
{
List<SelectListItem> items = new List<SelectListItem>();
string constr = ConfigurationManager.ConnectionStrings["section"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " SELECT Section_Name,Section_password, S_no  FROM section";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["Section_Name"].ToString(),
Value = sdr["S_No"].ToString()
});
}
}
con.Close();
}
}
return items;
}

尝试比较从获得的值

DropdownList1.SelectedValue;

使用WHERE子句从SQL中获得的值。

最新更新