I'm trying to get data from the database which i am able to do but i cant seem to pass it to the view's select option tag in html.
I've tried using a string and pasing it but it just passes the first value. havent been able to pass it through a string array either
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();
}
I want to be able to show it in this
<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>
tereško
58.5k26 gold badges100 silver badges151 bronze badges
1 Answer 1
Try to replace your html for this
@Html.DropDownList("value", new SelectList(ViewBag.mydata))
answered Aug 29, 2019 at 14:39
Sign up to request clarification or add additional context in comments.
Comments
default
@Html.DropDownListFor()
?