我正在尝试填充 HTML。selectlist
使用来自数据库调用的字符串值(位置地址)和文本(位置描述)字段填充的下拉列表。我将选择列表作为视图数据传递给我的视图。下拉列表填充得很好,但是当我使用该值时,它是 null 或空,正如我在我的 javascript 函数中放置的警报所看到的那样。这是代码任何想法为什么this.locations.value
为空:
我的查看代码:
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
function setDirections(fromAddress, toAddress, locale) {
alert(toAddress);
gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}
</script>
<div id="maincontent2">
<form action="#" onsubmit="setDirections(this.from.value, this.locations.value, 'en_US'); return false">
<table>
<tr>
<th align="left">From: </th>
<td align="left" ><input type="text" id="fromAddress" name="from" size="35px" value="King of Prussia, PA"/></td>
<th align="left"> To: </th>
<td align="left"> <%= Html.DropDownList("locations",(SelectList)ViewData["OfficeLocations"])%></td>
</tr>
<tr>
<td></td>
<td align="left">
<br />
<input name="submit" type="submit" value="Get Directions!" />
</td>
</tr>
</table>
<table>
<tr>
<td valign="top"><div id="drv_directions" style="width: 250px"></div></td>
<td valign="top" style="padding-top:15px"><div id ="map_canvas"></div></td>
</tr>
</table>
</form>
</div>
我的控制器代码:
public ActionResult Directions()
{
uls_dbDataContext ulsdb_dc = new uls_dbDataContext();
ViewData["OfficeLocations"] = new SelectList(ulsdb_dc.GetOfficeLocations(),"location_address", "location_name");
ViewData["Title"] = "Directions";
return View();
}