1

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
asked Aug 29, 2019 at 9:56
6

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.