javascript 在ViewData中使用选择列表填充下拉列表

4dbbbstv  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(83)

我正在尝试用selectlist填充HTML.dropdownlist,而selectlist是用字符串值填充的(位置地址)和文本(一个位置描述)字段从数据库调用。我传递selectlist作为viewdata到我的视图。下拉填充罚款,但是当我使用这个值的时候,它是null或者是空的,就像我在我的javascript函数中放置的一个警告所显示的那样。
我的视图代码:

<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:&nbsp;</th>
                <td align="left" ><input type="text" id="fromAddress" name="from" size="35px" value="King of Prussia, PA"/></td>
                <th align="left">&nbsp;&nbsp;To:&nbsp;</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();
}
jhdbpxl9

jhdbpxl91#

Locations是一个select,没有value属性。要获取所选选项的值,需要使用所选索引,找到正确的选项,并引用该选项的值。但是,如果使用jQuery,则可以它将使用val获取select的值()方法。我建议使用jQuery,因为它会使代码更简单,而且MS将在VisualStudio中支持它。
使用jQuery的示例:

<script type='text/javascript'>
    $(document).ready( function() {
       $('form').submit( function() {
          var fromAddress = $(this).find('#from').val();
          var toAddress = $(this).find('#locations').val();
          var locale = 'en-US';

          ....
          return false;
       });
    });
</script>

  <form action="#">

    <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>

    ...
qhhrdooz

qhhrdooz2#

下面是现在工作的代码:

<script type="text/javascript">

    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function setDirections(fromAddress, toAddress, locale) {

        gdir.load("from: " + fromAddress + " to: " + toAddress,
        { "locale": locale });
    }

    $(document).ready(function() {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map_canvas"));
            gdir = new GDirections(map, document.getElementById("drv_directions"));
            GEvent.addListener(gdir, "load", onGDirectionsLoad);
            GEvent.addListener(gdir, "error", handleErrors);

            setDirections("King of Prussia", "1302 Conshohocken Road, Conshohocken, PA 19428", "en_US");
        }
        $('form').submit(function() {
            var fromAddress = $(this).find('#from').val();
            var toAddress = $(this).find('#locations').val();
            var locale = 'en-US';
            alert(fromAddress);
            alert(toAddress);
            setDirections(fromAddress, toAddress, locale);
            return false;
        });

    });

</script>

相关问题