级联下拉列表填充脚本和返回列表的viewbag

时间:2016-05-12 07:25:55

标签: javascript jquery asp.net-mvc razor

我遇到了级联下拉菜单的问题。我也是脚本编写的新手,所以请不要这样做。 我在论坛中就同一个问题经历过各种帖子,但大多数都提供了这样的解决方案http://www.articlemirror.in/2014/01/populate-cascading-dropdown-list-in.html。但是,在我的情况下,我不希望脚本部分中有选项标记(我不确定它是否可能)。请帮忙!

脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $("#U_ICountry").change(function () {
          var ID = $("#U_ICountry").val();

            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetSData","institute")',
                data: { ID: ID },
                dataType: 'json',
                success: function (data) {
               //  how to proceed from here?                       
                    if (data.success) {

                       //how to append?
                    }
                    else {

                    }
                }
            });
        });
    });
</script>

以下是观点:

         <div class="form-group">
              @Html.LabelFor(model => model.U_ICountry, new { @class = "control-label" })
              <div class="row">
                  <div class="col-xs-3">
                      @Html.DropDownList("U_ICountry", null, "-- Select Status --", new { @class = "form-control" })                          
                  </div>
              </div>
          </div>

          <div class="form-group">
              @Html.LabelFor(model => model.U_IState, htmlAttributes: new { @class = "control-label" })
              <div class="row">
                  <div class="col-xs-3">
                     @Html.DropDownList("U_IState", null, "-- Select Status --", new { @class = "form-control" })  
                  </div>
                  </div>
              </div>

              <div class="form-group">
                  @Html.LabelFor(model => model.U_ICity, htmlAttributes: new { @class = "control-label" })
                  <div class="col-md-10">
                       @Html.DropDownList("U_ICity", null, "-- Select Status --", new { @class = "form-control" })  
                  </div>
              </div>

型号:

    public partial class C_SLM_CNRY //for country
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int U_CId { get; set; }
        public string U_CSort { get; set; }
        public string U_Name { get; set; }
    }

    public partial class C_SLM_STAT //for state
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int U_SId { get; set; }
        public string U_Name { get; set; }
        public int U_CId { get; set; }    
    }

    public partial class C_SLM_CITY //for city
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int U_PId { get; set; }
        public string U_Name { get; set; }
        public int U_SId { get; set; }        
    }

    public partial class C_SLM_OINS
    {
        [Key]
        [Display(Name = "Code")]
        public string U_ICode { get; set; }
        [Display(Name = "Name")]
        public string U_IName { get; set; }
        [Display(Name = "Address")]
        [DataType(DataType.MultilineText)]
        public string U_IAddress { get; set; }
        [Display(Name = "Country")]
        public string U_ICountry { get; set; }
        [Display(Name = "State")]
        public string U_IState { get; set; }
        [Display(Name = "City")]
        public string U_ICity { get; set; }
        [Display(Name = "Pincode")]            
        [Display(Name = "Vat Number")]
        public string U_IVat { get; set; }
        [Display(Name = "Service Tax Number")]
        public string U_ISrvTax { get; set; }

        public virtual ICollection<C_SLM_OSCH> C_SLM_OSCH { get; set; }
    }
}

控制器:

        public ActionResult create()
        {
            ViewBag.U_ICountry = new SelectList(db.Country, "U_CId", "U_Name");
            return View();
        }

        public JsonResult GetSData(int ID)
        {
            var cQuery = from e in db.State
                                    where e.U_CId == ID
                                    select e;
            ViewBag.State = new SelectList(cQuery, "U_SId", "U_Name");
            return Json(new { json = ViewBag.State });              

        }

        public JsonResult GetCData(int ID)
        {
            var sQuery = from e in db.City
                                   where e.U_CId == ID
                                   select e;
            ViewBag.City= new SelectList(sQuery, "U_PId", "U_Name");
            return Json(new { json = ViewBag.City});              

         }

0 个答案:

没有答案