2016-06-24 71 views
0

我有國家和州dropdowns.Country和州有關係countryid級聯下拉角度與它們之間的關鍵關係

我在獲取這兩個級聯下拉列表的數據時出錯。

下面是控制器爲國家返回JsonResult的錯誤圖像。

enter image description here

角功能:

var getdata = fac.GetCountry = function() { 
    return $http.get('/Data/GetCountries') 
}; 
getdata.then(function (d) { 
    $scope.CountryList = d.data; 
}, function (error) { 
    alert('Error!'); 
}); 

控制器:

public JsonResult GetCountries() 
{ 
    List<Country> allCountry = new List<Country>(); 
    using (SunilEntities dc = new SunilEntities()) 
    { 
     allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList(); 
    } 
    return new JsonResult { Data = allCountry, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; 
    //return Json(allCountry, JsonRequestBehavior.AllowGet); 
} 

查看:

<div ng-controller="dropdowns"> 
    Country : <select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()"> 
       <option value="">Select Country</option> 
       </select> 
    State : <select ng-model="StateID" ng-options="I.StateID as I.StateName for I in StateList"> 
       <option value="">{{StateTextToShow}}</option> 
      </select> 
    <input type="button" value="Get Selected Values" ng-click="ShowResult()"/> 
    <div style="padding:10px; font-weight:bold; border:1px solid #f3f3f3"> 
     {{Result}} 
    </div> 
</div> 

型號:

public partial class Country 
{ 
    public Country() 
    { 
     this.States = new HashSet<State>(); 
    } 

    public int CountryID { get; set; } 
    public string CountryName { get; set; } 

    public virtual ICollection<State> States { get; set; } 
} 

public partial class State 
{ 
    public int StateID { get; set; } 
    public string StateName { get; set; } 
    public Nullable<int> CountryID { get; set; } 

    public virtual Country Country { get; set; } 
    public virtual State State1 { get; set; } 
    public virtual State State2 { get; set; } 
} 

public DbSet<Country> Countries { get; set; } 
public DbSet<State> States { get; set; } 

回答

0

我得到了解決辦法。

通過改變行動(控制器)有點我得到了結果。

控制器: -

 public JsonResult GetCountries() 
     {   
      using (SunilEntities dc = new SunilEntities()) 
      { 
       var ret = dc.Countries.Select(x => new { x.CountryID, x.CountryName }).ToList(); 
       return Json(ret, JsonRequestBehavior.AllowGet);    

      }    
     } 

     // Fetch State by Country ID 
     public JsonResult GetStates(int countryID) 
     {    
      using (SunilEntities dc = new SunilEntities()) 
      {     
       var ret = dc.States.Where(x => x.CountryID == countryID).Select(x => new { x.StateID, x.StateName }).ToList(); 
       return Json(ret, JsonRequestBehavior.AllowGet); 
      }    
     } 

我的國家和國家dropdowns.Country和國家對coutryid關係。

我在獲取這兩個級聯下拉列表的數據時出錯。

下面是控制器爲國家返回JsonResult的錯誤圖像。

角funtion: -

var getdata= fac.GetCountry = function() { 
     return $http.get('/Data/GetCountries') 
    }; 
    getdata.then(function (d) { 
     $scope.CountryList = d.data; 
    }, function (error) { 
     alert('Error!'); 
    }); 

查看: -

<div ng-controller="dropdowns"> 
    Country : <select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()"> 
       <option value="">Select Country</option> 
       </select> 
    State : <select ng-model="StateID" ng-options="I.StateID as I.StateName for I in StateList"> 
       <option value="">{{StateTextToShow}}</option> 
      </select> 
    <input type="button" value="Get Selected Values" ng-click="ShowResult()"/> 
    <div style="padding:10px; font-weight:bold; border:1px solid #f3f3f3"> 
     {{Result}} 
    </div> 
</div> 

型號: -

public partial class Country 
    { 
     public Country() 
     { 
      this.States = new HashSet<State>(); 
     } 

     public int CountryID { get; set; } 
     public string CountryName { get; set; } 

     public virtual ICollection<State> States { get; set; } 
    } 

    public partial class State 
    { 
     public int StateID { get; set; } 
     public string StateName { get; set; } 
     public Nullable<int> CountryID { get; set; } 

     public virtual Country Country { get; set; } 
     public virtual State State1 { get; set; } 
     public virtual State State2 { get; set; } 
    } 

    public DbSet<Country> Countries { get; set; } 
    public DbSet<State> States { get; set; }