【问题标题】:cascading dropdown in angular with key relation between them角度级联下拉菜单,它们之间有关键关系
【发布时间】:2016-10-26 15:25:33
【问题描述】:

我有国家和州dropdowns.Country 和州有关系countryid

我在为这两个级联下拉菜单获取数据时遇到错误。

以下是控制器返回国家/地区的 JsonResult 时出错的图像。

角函数:

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; }

【问题讨论】:

    标签: angularjs entity-framework model-view-controller


    【解决方案1】:

    我得到了解决方案。

    通过稍微改变操作(控制器)我得到了结果。

    控制器:-

            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);
                }              
            }
    

    我有国家和州的下拉菜单。国家和州在 coutryid 上有关系。

    我在获取这两个级联下拉列表的数据时遇到错误。

    以下是控制器返回国家/地区的 JsonResult 时出错的图像。

    角函数:-

       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; }
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多