PDA

View Full Version : Lỗi [WebMethod]



duongthienhoang
03-08-2011, 10:58
Chào các bạn mình làm 2 dropdownlist depend, chọn 1 droplist chọn tỉnh, thì 1 droplist khác sẽ chọn huyện tương ứng vs tỉnh đó. mình có 2 model như sau
public class Provinces
{
[Key]
public int ProvinceID { get; set; }
public string ProvinceName { get; set; }
public string area_code { get; set; }
public string Zip_code { get; set; }
public string Number_plate { get; set; }
public float Area { get; set; }
public float Population { get; set; }
public string URLMap { get; set; }
public string Region { get; set; }
public string Description { get; set; }
public ICollection<Districts> Districts { get; set; }
}
public class Districts
{
[Key]
public int DistrictID { get; set; }
public string DistricName { get; set; }
[DisplayName("Provinces")]
public int ProvinceID { get; set; }
public string area_code { get; set; }
public string Zip_code { get; set; }
public string Number_plate { get; set; }
public float Population { get; set; }
public string URLMap { get; set; }
public float Area { get; set; }
public string Description { get; set; }
public virtual Provinces Provinces { get; set; }
}
mình tao phương thức webmethod như sau
public List<Districts> GetDistrict(int ID)
{
var db = new MyLocationEntities();
var ds = from m in db.Districts where m.ProvinceID == ID select m;
return ds.ToList();
}
và script
<script type="text/javascript">

var ddlpr;
var ddlds;

function pageLoad() {
ddlpr = $get("ProvinceID");
ddlds = $get("ds");
$addHandler(ddlpr, "change", bindOptions);
bindOptions();
}

function bindOptions() {
ddlds.options.length = 0;
var ID = ddlpr.value;
if (ID)
{
Sys.Net.WebServiceProxy.invoke("../Services/District.asmx", "GetDistrict", false, { ID: ID }, bindOptionResults,fail);
}
}
function fail() {
alert("Lỗi");
}
function bindOptionResults(data) {

var newOption;
alert(data.length)
for (var k = 0; k < data.length; k++)
{
newOption = new Option(data[k].DistricName, data[k].DistrictID);
ds.options.add(newOption);

}
ds.options.add(newOption);

}

</script>
mình kiểm tra khi webmethod trả vể giá trị null thì mới gọi được hàm bindOptionResults, còn khi trả về mảng giá trí thì ko gọi được hàm này, mình không biết tải sao? các bạn giúp mình với, mình cảm ơn nhiều.