我有一套预定义的下拉菜单,我需要在viewdata对象中选择下拉列表,但是不知道如何选择.aspx页面中的选项
这里我的菜单看起来如何
<td headers="Vehicle" style="background-color:#EFF3FB;font-family: Verdana; font-size: 10px;"><select>
<option value="Car">Car</option>
<option value="Bus">Bus</option>
<option value="Lorry">Lorry</option>
<option value="Truck">Truck</option>
</select>
</td>
以下ViewData.Model.VehicleDetails.Vehicle
将需要选择车辆。但是不要如何在aspx页面中选择它。
答案 0 :(得分:0)
在您的模式中为列表添加另一个属性:
public class YourModel
{
public SelectList VehiclesList {get;set;}
public string SelectedVehicle {get;set;}
}
在行动中:
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem{Text="Car",Value="Care"});
list.Add(new SelectListItem{Text="Bus",Value="Bus"});
list.Add(new SelectListItem{Text="Truck",Value="Truck"});
yourModel.VehiclesList = new SelectList(list,"Value","Text", ViewData.Model.VehicleDetails.Vehicle);
在视图中:
<%: Html.DropDownListFor(model => model.SelectedVehicle , Model.VehiclesList)%>
<强>更新强>
你也可以这样做:
<%= Html.DropDownList(ViewControlNames.vehicle,
new SelectList(new List<SelectListItem> {
new SelectListItem{ Text="Car", Value = "car" },
new SelectListItem{ Text="Bus", Value = "Bus" },
new SelectListItem{ Text="Truck", Value = "Truck" } },"Bus"),
new { id = ViewControlNames.vehicle, Class = "ShortInputField"})) %>
答案 1 :(得分:0)
<select>
<%if (ViewData.Model.Details.ElementAt(i).vehicle == "Car")%>
<%{%>
<option value="car" selected="selected">car</option>
<option value="Bus">Bus</option>
<option value="Lorry">Lorry</option>
<%} %>
<%else if (ViewData.Model.Details.ElementAt(i).vehicle == "Bus")%>
<%{%>
<option value="car" >car</option>
<option value="Bus" selected="selected">Bus</option>
<option value="Lorry">Lorry</option>
<%} %>
<%else if (ViewData.Model.Details.ElementAt(i).vehicle == "Lorry")%>
<%{%>
<option value="car">car</option>
<option value="Bus">Bus</option>
<option value="Lorry" selected="selected">Lorry</option>
<%} %>
</select>