0

I have 2 HTML select using jQuery multi-select cascade bind using ajax call it works fine the problem is I want to set value in master then bind detail then set value in detail but since it is ajax when try to set the value in detail the values not yet available.

Table Cars CarID int CarModel(Toyota) CarSubModel(yaris)

 function GetCarData()
    {
        var CarID = getQuerystringByName('C');
        if (CarID!=null)
        {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Query.asmx/GetCarByID",
                data: '{CarID: ' + CarID + '}',
                dataType: "json",
                success: function (Result) {
                    Result = Result.d;
                    txtCarName.value = Result.CarName;
                    $("#ddlCarModel").val(Result.ModelID);
                      BindSubModel();// Ajax to load Sub Models
                      $("#ddlCarSubModel").val(Result.SubModelID);

I need to (some how) execute $("#ddlCarSubModel").val(Result.SubModelID); after the sub ajax complete i do not want to use wait methods(Timers).

  function BindSubModel() {
        var ModelVal = $("#ddlCarModel").val();
        if (ModelVal != "") {
                $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Query.asmx/LoadCarSubModel",
            data: '{ModelID: ' + ModelVal + '}',
            dataType: "json",
            success: function (Result) {

the web Method

     public class CarSubModelInfo
    {
        public int SubModelID { get; set; }
        public string SubModelName { get; set; }
    }

    [WebMethod]
    [ScriptMethod]
    public List<CarSubModelInfo> LoadCarSubModel(string ModelID)
    {
        string Conn = System.Configuration.ConfigurationManager.ConnectionStrings["GPS_TrackingConnectionString"].ConnectionString;

        CarSubModelInfo driver = new CarSubModelInfo();
        List<CarSubModelInfo> SubModelInformation = new List<CarSubModelInfo>();
        DataSet ds;
        using (SqlConnection con = new SqlConnection(Conn))
        {
            using (SqlCommand cmd = new SqlCommand("select * from T_SubCarModels where  ModelID=" + ModelID, con))
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {

                    ds = new DataSet();
                    da.Fill(ds);
                }
            }
        }
        try
        {
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            SubModelInformation.Add(new CarSubModelInfo()
                            {
                                SubModelID = Convert.ToInt32(dr["id"]),
                                SubModelName = dr["name"].ToString()
                            });
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return SubModelInformation;
    }
Maher Khalil
  • 529
  • 1
  • 15
  • 28

1 Answers1

0

Put $("#ddlCarSubModel").val(Result.SubModelID); Into success in the function BindSubModel();

Gopinath Shiva
  • 3,822
  • 5
  • 25
  • 48
  • Result is the success of the prev ajax it will not be available in bindSubModel – Maher Khalil Apr 26 '17 at 10:48
  • send it there, as param BindSubModel(Result.SubModelID); ` function BindSubModel(SubModelID) { ... success: function (Result) { $("#ddlCarSubModel").val(SubModelID);` – Alexander Fast Apr 26 '17 at 11:00