【发布时间】:2018-01-02 18:20:39
【问题描述】:
我正在尝试在视图中填充下拉列表。我在使用剃刀语法时遇到了问题(我相信——这个框架只用了几天)。我已经看到了多种方法来做到这一点;但是,我有兴趣让它发挥作用,因此我可以从中学习......:)。这是我所拥有的: 我的数据访问类:
public class DropDown{
public List<SelectListItem> Get_State() //State
{
using (SqlConnection conSql = new SqlConnection(conStr))
{
if (conSql.State == ConnectionState.Closed)
{
conSql.Open();
}
SqlCommand com = new SqlCommand("Select * From Select_State Order by State", conSql);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
List<SelectListItem> stateList = new List<SelectListItem>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
stateList.Add(new SelectListItem { Text = dr["StateName"].ToString(), Value = dr["State"].ToString() });
}
return stateList;
}
}
我的模特:
public class DropDownModel
{
public List<SelectListItem> State { get; set; }
public List<SelectListItem> extra1{ get; set; }
public List<SelectListItem> extra2{ get; set; }
public List<SelectListItem> extra3{ get; set; }
public List<SelectListItem> extra4{ get; set; }
}
}
我的控制器:
public IActionResult DropDown() {
DropDownModel dropdown = new DropDownModel();
dropdown.State = Get_DropDown.Get_State();
return View(dropdown);
}//DropDown
如何获得 razor 语法以显示包含状态的下拉列表?
【问题讨论】: