You need to databind the dropdown list with a data source. Here's one tutorial: http://www.progtalk.com/viewarticle.aspx?articleid=194
To retrieve the selected value: Specify an event handler like a submit button or even the ddl itself once selction is made. Then retrieve in your code behind something like:
mattsgrandad
Member
14 Points
18 Posts
how to code a dropdown list on a cshtml page
Oct 20, 2011 09:26 PM|LINK
Does anyone have an example of coding for a dropdown list on a form on a cshtml page.
Two things specifically:
1. Dropdown list to be prepopulated from a database table (an sdf file)
2. How do I then access the selecetd value from the form
Any help gratefully received.
Thanks
dropdown
BlogDog
Member
456 Points
333 Posts
Re: how to code a dropdown list on a cshtml page
Oct 21, 2011 03:03 AM|LINK
You need to databind the dropdown list with a data source. Here's one tutorial: http://www.progtalk.com/viewarticle.aspx?articleid=194
To retrieve the selected value: Specify an event handler like a submit button or even the ddl itself once selction is made. Then retrieve in your code behind something like:
dim selectedValue as string
selectedValue = ddl1.SelectedValue.ToString
HTH
dropdown
mattsgrandad
Member
14 Points
18 Posts
Re: how to code a dropdown list on a cshtml page
Oct 24, 2011 08:50 AM|LINK
Although it didn't provide the answer, it set me on the right path so thanks for that.
Because my page is cshtml I was looking for cshtml compatible code.
The eventual solution I came up is as follows (dropdown populated with a list of 'FieldA' from 'TableA'):
@{
// Retrieve records for the list
var db = Database.Open("StarterSite");
var selectrecords = "SELECT * FROM TableA ";
var selected = "";
if(IsPost){
selected = Request.Form["dropdown"];
}
}
<body>
<form method="post" action="">
<fieldset>
<div>
<select name ="dropdown">
@foreach(var recordrow in db.Query(selectrecords))
{
<option>@recordrow.fieldA</option>
}
</select>
</div>
<div>
<label> </label>
<input type="submit" value="Update" class="submit" />
</div>
</fieldset>
</form>
<div>
Selected value is @selected
</div>
dropdown