asp.net mvc 如何把下拉列表获得的值传入数据库中?
控制器:publicActionResultJiaBanAdd(){List<SelectListItem>JiaBanType=JiaBanInfoDAL.Select...
控制器:
public ActionResult JiaBanAdd()
{
List<SelectListItem> JiaBanType = JiaBanInfoDAL.SelectList("JBLX");
ViewBag.JBType = JiaBanType;
List<SelectListItem> department = JiaBanInfoDAL.SelectList("BMLX");
ViewBag.ZWType = department;
List<SelectListItem> post = JiaBanInfoDAL.SelectList("ZWLX");
ViewBag.BmType = post;
return View();
}
Model
public static List<SelectListItem> SelectList(string Type)
{
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
string sql = "select * from TypeConfing where Type = '" + Type + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter adt = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
adt.Fill(dt);
SqlDataReader sdr = cmd.ExecuteReader();
List<SelectListItem> select = new List<SelectListItem>();
if (null == dt)
{
return select;
}
for (int i = 0; i < dt.Rows.Count; i++)
{
SelectListItem ty = new SelectListItem();
ty.Value = dt.Rows[i]["ItemID"].ToString();
ty.Text = dt.Rows[i]["ItemName"].ToString();
select.Add(ty);
}return select;
}
View
<div class="editor-field">
@Html.DropDownList("ItemName", ViewBag.ZWType as IEnumerable<SelectListItem>)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.bm)
</div>
<div class="editor-field">
@Html.DropDownList("ItemName", ViewBag.BmType as IEnumerable<SelectListItem>)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.type)
</div>
<div class="editor-field">
@Html.DropDownList("ItemName", ViewBag.JBType as IEnumerable<SelectListItem>)
</div> 展开
public ActionResult JiaBanAdd()
{
List<SelectListItem> JiaBanType = JiaBanInfoDAL.SelectList("JBLX");
ViewBag.JBType = JiaBanType;
List<SelectListItem> department = JiaBanInfoDAL.SelectList("BMLX");
ViewBag.ZWType = department;
List<SelectListItem> post = JiaBanInfoDAL.SelectList("ZWLX");
ViewBag.BmType = post;
return View();
}
Model
public static List<SelectListItem> SelectList(string Type)
{
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
string sql = "select * from TypeConfing where Type = '" + Type + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter adt = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
adt.Fill(dt);
SqlDataReader sdr = cmd.ExecuteReader();
List<SelectListItem> select = new List<SelectListItem>();
if (null == dt)
{
return select;
}
for (int i = 0; i < dt.Rows.Count; i++)
{
SelectListItem ty = new SelectListItem();
ty.Value = dt.Rows[i]["ItemID"].ToString();
ty.Text = dt.Rows[i]["ItemName"].ToString();
select.Add(ty);
}return select;
}
View
<div class="editor-field">
@Html.DropDownList("ItemName", ViewBag.ZWType as IEnumerable<SelectListItem>)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.bm)
</div>
<div class="editor-field">
@Html.DropDownList("ItemName", ViewBag.BmType as IEnumerable<SelectListItem>)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.type)
</div>
<div class="editor-field">
@Html.DropDownList("ItemName", ViewBag.JBType as IEnumerable<SelectListItem>)
</div> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询