【发布时间】:2019-08-29 09:56:47
【问题描述】:
我正在尝试从数据库中获取数据,但我似乎无法将其传递给 html 中视图的选择选项标签。
我尝试过使用字符串并对其进行粘贴,但它只是传递了第一个值。也无法通过字符串数组传递它
public ActionResult GetDetails()
{
SqlConnection con;
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
con = new SqlConnection(constr);
con.Open();
string sqlCommand = "SELECT *FROM cityname";
SqlCommand cmd = new SqlCommand(sqlCommand, con);
SqlDataReader read;
read = cmd.ExecuteReader();
List<string> str = new List<string>();
while(read.Read())
{
str.Add(read.GetValue(1).ToString());
}
ViewBag.mydata = str;
return View();
}
我希望能够在此展示它
<select class="mdb-select md-form" id="PersonCity" name="PersonCountry" onchange="run1();">
<option value="" disabled selected>Choose your Country</option>
<option value='$country_name'>$country_name</option>
【问题讨论】:
-
我无法迭代视图包。我希望能够将它发送到 html 中的选择选项,然后通过 javascript 进行迭代
-
为什么不用辅助方法
@Html.DropDownListFor()? -
我不知道如何使用辅助方法。我是 mvc 和 asp.net 的新手
标签: c# html asp.net-mvc asp.net-mvc-5