如何使用mvc中的另一个下拉列表输入填充下拉列表

时间:2017-07-27 09:47:55

标签: c# asp.net-mvc sql-server-2008

我有2次下拉叫做药物和力量。在选择药物时,强度下降应根据药物下降的输入填充。 HTML代码:

<label>Medication<sup>*</sup></label> @Html.DropDownListFor( Model => Model.medication, Model.medication, htmlAttributes: new { id = "MyId" , style = "width:200px; height :30px"})   <button type="button" style="height:30px;" onclick="return openmedication()">search</button>                                
<label>Strength</label>@Html.DropDownListFor(Model => Model.strength, Model.strength, "Select Strength", new { style = "width:200px; height :30px" })  

**当我运行此代码时,我从表中获得所有强度下拉值。没有根据药物排序值。

我的模特:**

 public List<ItemModel> med()//FOR MEDICATION
    {

        List<ItemModel> itemList = new List<ItemModel>();
        connection();
        SqlCommand cmd = new SqlCommand("procmedication_dropdown1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PstrOperationFlag", "S-drugname");
        con.Open();
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sd.Fill(dt);
        ItemModel item = new ItemModel();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            ItemModel io = new ItemModel();
            while (sdr.Read())
            {
                io = new ItemModel();
                io.medicat = sdr["drugname"].ToString();


                itemList.Add(io);

            }
        }
        con.Close();
        return itemList;
    }
  public List<SelectListItem> Add()// FOR STRENGTH
    {

        List<SelectListItem> items = new List<SelectListItem>();
        connection();

        SqlCommand cmd = new SqlCommand("procmedication_dropdown1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@pstroperationflag", "S-strength");

        con.Open();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                items.Add(new SelectListItem
                {
                    Text = sdr["strength"].ToString(),
                    Value = sdr["strength"].ToString()
                });
            }
        }
        con.Close();
        return items;
    }

2 个答案:

答案 0 :(得分:0)

你可以使用像这样的jquery来实现这个目的。

 $(document).ready(function(){
    $("#medication").on("change",function(){
       getStrengthList();
    });
 });

function getStrengthList() {
    var url = "Add" // Url of controller

    $.ajax({
        url: url,
        timeout: 500000,
        type: "GET",
        beforeSend: ShowLoader,
        success: function (data) {
            HideLoader();
            var selectList = $('#strength');
            selectList.find('option').remove().end();

            if (data.length > 0) {
                var selected = "";
                var selectedName = "";
                var i = 0;
                $.each(data, function (key, data) {
                    selectList.append('<option value="' + data.Value + '">' + data.Text + '</option>');

                    if (i == 0) {
                        selected = data.Value;
                        selectedName = data.Text;
                        i++;
                    }
                });
                selectedCategory = selected;
                selectedCategoryName = selectedName.toLowerCase();
                selectList.val(selected);
            }

        },
        complete: function () {

        },
        error:  function () {

        },
    });
}

答案 1 :(得分:0)

尝试以下代码

查看:

 public String[] getPlaces(){
        SQLiteDatabase db = this.getReadableDatabase();

        String [] columns = {"place1"};
        c = db.query("rates_table", columns, null, null, null, null, null);

        c.moveToFirst();
        ArrayList<String> places = new ArrayList<String>();
        while(!c.isAfterLast()) {
            places.add(c.getString(c.getColumnIndex("place1")));
            c.moveToNext();
        }
        c.close();
        return places.toArray(new String[places.size()]);

    }

脚本:

//Load Medication Data on Page Load
    @Html.DropDownListFor( Model => Model.medication, Model.medication, htmlAttributes: new { id = "MyId" , style = "width:200px; height :30px"}) 

//Load Strength data on change of medication from ajax call                             
    @Html.DropDownListFor(Model => Model.strength, Model.strength, "Select Strength", new { style = "width:200px; height :30px" })  

控制器:

   $("#MyId").on('change', function() {
   //Get the value of Medication dropdown
     var medicationId = $("#MyId").val();
    $.ajax({
            url: '@Url.Action("GetStrengthDropdown","Controller")',
            type: 'GET',
            dataType: 'json',
             data:medicationId :medicationId ,
             success: function(data) {

            $.each(data, function (i) {
                var optionhtml = '<option value="' +
                data[i].Value + '">' +data[i].Text + '</option>';
                $("#strength").append(optionhtml);
            });

            }
        });
    });