Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating dropdownlist with selectlist in ViewData
    primarykey
    data
    text
    <p>I am trying to populate an HTML.Dropdownlist with a selectlist which is populated with a string value (a location address) and text (a location description) fields from a database call. I am passing the selectlist as viewdata to my view. The dropdown populates fine but when I go to use the value it is null or empty as seen by an alert I put in my javascript function. Here is the code Any ideas why "this.locations.value" is null:</p> <p>My View code:</p> <pre><code>&lt;script type="text/javascript"&gt; var map; var gdir; var geocoder = null; var addressMarker; function setDirections(fromAddress, toAddress, locale) { alert(toAddress); gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale }); } &lt;/script&gt; &lt;div id="maincontent2"&gt; &lt;form action="#" onsubmit="setDirections(this.from.value, this.locations.value, 'en_US'); return false"&gt; &lt;table&gt; &lt;tr&gt; &lt;th align="left"&gt;From:&amp;nbsp;&lt;/th&gt; &lt;td align="left" &gt;&lt;input type="text" id="fromAddress" name="from" size="35px" value="King of Prussia, PA"/&gt;&lt;/td&gt; &lt;th align="left"&gt;&amp;nbsp;&amp;nbsp;To:&amp;nbsp;&lt;/th&gt; &lt;td align="left"&gt; &lt;%= Html.DropDownList("locations",(SelectList)ViewData["OfficeLocations"])%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td align="left"&gt; &lt;br /&gt; &lt;input name="submit" type="submit" value="Get Directions!" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;table&gt; &lt;tr&gt; &lt;td valign="top"&gt;&lt;div id="drv_directions" style="width: 250px"&gt;&lt;/div&gt;&lt;/td&gt; &lt;td valign="top" style="padding-top:15px"&gt;&lt;div id ="map_canvas"&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>My Controller Code:</p> <pre><code>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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload