2017-04-24 74 views
-1

我怎样才能在数据库uisng下拉列表分类的onces如何知道类别下拉选择的子类别?

我点击INVORG类别,并显示在可用DEPTCODE?

using Json 
 

 
[HttpPost] 
 
     public JsonResult GetDEPTCODE(string id) 
 
     { 
 
      List<SelectListItem> states = new List<SelectListItem>(); 
 
      /// 
 

 
//I got Error Code, can you please define my wrong code and correct thanks 
 

 
      states = from u in _db.USERGROUPs where u.INVORG == id 
 
        select(new SelectListItem {Text =u.INVORG, Value = u.DEPTCODE}); 
 

 

 
     return Json(new SelectList(states, "Value", "Text")); 
 
     }

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script type="text/javascript"> 
 
    $(document).ready(function() { 
 
     //Dropdownlist Selectedchange event 
 
     $("#INVORG").change(function() { 
 
      $("#DEPTCODE").empty(); 
 
      $.ajax({ 
 
       type: 'POST', 
 
       url: '@Url.Action("GetDEPTCODE")', // we are calling json method 
 
       dataType: 'json', 
 
       data: { id: $("#INVORG").val() }, 
 
       // here we are get value of selected INVORG and passing same value 
 
       success: function (states) { 
 
        // states contains the JSON formatted list 
 
        // of states passed from the controller 
 
        $.each(states, function (i, state) { 
 
         $("#DEPTCODE").append('<option value="' + state.Value + '">' + 
 
         state.Text + '</option>'); 
 
         // here we are adding option for States 
 
        }); 
 
       }, 
 
       error: function (ex) { 
 
        alert('Failed to retrieve states.' + ex); 
 
       } 
 
      }); 
 
      return false; 
 
     }) 
 
    }); 
 
</script>

回答

0

改变你的方法,以作为HTTPGET你得到的数据。

[HttpGet] 
    public ActionResult GetDEPTCODE(string id) 
    { 
     List<SelectListItem> items= new List<SelectListItem>(); 
     return Json(new { data = items}, JsonRequestBehavior.AllowGet); 
    } 

改变你的脚本如下

$("#INVORG").change(function() { 
     $("#DEPTCODE").empty(); 
     $.ajax({ 
      type: 'GET', 
      url: '@Url.Action("GetDEPTCODE")', // we are calling json method 
      dataType: 'json', 
      data: { id: $("#INVORG").val() }, 
      // here we are get value of selected INVORG and passing same value 
      success: function (data) { 
       // states contains the JSON formatted list 
       // of states passed from the controller 
       $("#DEPTCODE").html(''); 
       $.each(data["data"], function (key, value) { 

       $("#DEPTCODE").append($("<option> 
     </option>").val(value.Value).html(value.Text)); 

      }); 
      }, 
      error: function (ex) { 
       alert('Failed to retrieve states.' + ex); 
      } 
     }); 
     return false; 
    }) 
+0

先生你好你的代码没有工作,invorg下不会出现 –

+0

看到这个[链接](https://dotnetfiddle.net/1bPZym) – Learner

+0

这个答案是张贴为您原来的问题..你可以改变整个问题? @ThePrograMMER – Learner