0

我有一个带有选择的下拉菜单,我可以在其中设置已完成的下拉菜单的默认值,下次当用户登陆该页面时,我需要显示已保存的下拉菜单

html

<select id="ddlValue" onchange="OnChange()" style="height: 25px;padding: 2px 12px;font-size: 12px;color: #555;background-color: #fff;background-image: none;border: 1px solid #ccc;width:15%;" class="ItemPT5">
                        <option value="0">All</option>
                        <option value="2">Value1</option>
                        <option value="1">Value2</option>
                        <option value="3">Value3</option>
</select>

jQuery代码-

$.ajax({
                type: "GET",
                url: webAPIURL + '/GetLoggedinuserId?MemberId=' + UserId,
                dataType: "json",
                async: true,
                cache: false,
                traditional: true,
                contentType: "application/json; charset=utf-8",
                success: function (data) {   //getting all the data from controller 
                    if (data.length > 0) {  //since length will be greater than 0 it satisfies if conditon
                        $.each(data.d, function () {
                            $("#ddlValue").append($("<option/>").val(this.KeyName).text(this.ValueName));  //unable to bind selected value to drop down
                        });
                    }
                }
            });

数据库-

ID              DDL_TYPE        SELECTED
1                value1         true  //should bind value 1 to drop down using jquery since this is saved against the user
2                value2             false
3                value3         false

控制器-

public string GetLoggedinuserId(int MemberId)
        {
            try
            {
                esParameters EsPm = new esParameters();
                DataTable dt = null;
                EsPm.Add(new esParameter("ID", MemberId));
                dt = EsPm.FillDataTable("USP_GET");
            }
            catch (Exception ex) { ex.LogException(); return null; }
            return null;
        }
4

1 回答 1

0

模型

public class DropdownModel{
    public int Value {get; set;}
    public string Text {get; set;}
    public bool IsSelected {get; set;}
}

控制器

//add below namespace
using System.Web.Script.Serialization;

//method returns Json string
public string GetLoggedinuserId(int MemberId)
        {
            try
            {
                esParameters EsPm = new esParameters();
                DataTable dt = null;
                EsPm.Add(new esParameter("ID", MemberId));
                dt = EsPm.FillDataTable("USP_GET");
                //Convert DataTable to List
                List<DropdownModel> list = new List<DropdownModel>();
                //Iterate DataTable Rows and Add to List
                foreach(DataRow dr in dt.Rows)  
                {  
                     list.Add(new DropdownModel(){
                           Value = dr["ID"],
                           Text = dr["DDL_TYPE"],
                           IsSelected = dr["SELECTED"]
                     });

                }
                //Create JavaScriptSerializer object
                var jsonSerialiser = new JavaScriptSerializer();
                //Convert list to Json string
                var jsonString = jsonSerialiser.Serialize(list);
               //returns list as JsonString
               return jsonString;
            }
            catch (Exception ex) { 
                 ex.LogException();  
                 return null;
             }
             return null;
        }

JS

$(document).ready(function(){ 
      var options = '';
      $.ajax({
                type: "GET",
                url: webAPIURL + '/GetLoggedinuserId?MemberId=' + UserId,
                dataType: "json",
                async: true,
                cache: false,
                traditional: true,
                contentType: "application/json; charset=utf-8",
                success: function (data) {   //getting all the data from controller          //data is not null
                    if (data != null) {  //since length will be greater than 0 it satisfies if conditon
                         //parse Json string and iterate 
                         $.each(JSON.parse(data), function (i,v) {
                             //if user selected this option add selected attribute to option
                             var selected = v.IsSelected == true ? "selected": "";
                             //add options
                             options += "<option value="+ v.Value+" "+ selected +">"+ v.Text+"</option>";
                        });
                         //clear dropdown options
                         $("#ddlValue").empty();
                        //append options
                        $("#ddlValue").append(options)
                    }
                }
            });
});

html

<select id="ddlValue" onchange="OnChange()" style="height: 25px;padding: 2px 12px;font-size: 12px;color: #555;background-color: #fff;background-image: none;border: 1px solid #ccc;width:15%;" class="ItemPT5">
      <option value="0">All</option>
      <option value="2">Value1</option>
      <option value="1">Value2</option>
      <option value="3">Value3</option>
</select>
于 2019-08-01T06:45:48.910 回答