2015-09-29 34 views
0
public class CityDto : IEnumerable<CityDto> 
{ 
    public IEnumerator<CityDto> AllCities { get; set; } 

    private List<CityDto> _CityDto; 

    public IEnumerator<CityDto> GetEnumerator() 
    { 
     return _CityDto.GetEnumerator(); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return _CityDto.GetEnumerator(); 
    } 

    #region Constructors 

    /// <summary> 
    /// Initializes a new instance of the CityMasterdto class. 
    /// </summary> 
    public CityDto() 
    { 
    } 

    /// <summary> 
    /// Initializes a new instance of the CityMasterdto class. 
    /// </summary> 
    public CityDto(string city_name, long state_id, bool active) 
    { 
     this.City_name = city_name; 
     this.State_id = state_id; 
     this.Active = active; 
    } 

    #endregion 

    #region Properties 
    /// <summary> 
    /// Gets or sets the state name value. 
    /// </summary> 
    public string State_name { get; set; } 

    /// <summary> 
    /// Gets or sets the City_id value. 
    /// </summary> 
    public long City_id { get; set; } 

    /// <summary> 
    /// Gets or sets the City_name value. 
    /// </summary> 
    [Display(Name="City Name")] 
    [Required] 
    public string City_name { get; set; } 

    /// <summary> 
    /// Gets or sets the State_id value. 
    /// </summary> 
    public long State_id { get; set; } 
     public bool Active { get; set; } 

    #endregion 
} 

這是我的控制器代碼:沒能獲得視圖模型的IEnumerable集合,當前枚舉始終是空

public ActionResult index() 
{ 
    return View(GetCities(obj.Details()).AsEnumerable()); 
} 

private IEnumerable<CityDto> GetCities(DataSet ds) 
{ 
     foreach (DataRow row in ds.Tables[0].Rows) 
     { 
      yield return new CityDto 
      { 
       City_id = Convert.ToInt32(row["city_id"]), 
       City_name = Convert.ToString(row["city_name"]), 
       State_name = Convert.ToString(row["state_name"]), 
       State_id = Convert.ToInt32(row["state_id"]) 
      }; 
     } 
} 

這裏沒有任何編譯錯誤,但是當我運行的代碼,我得到一個錯誤:

The model item passed into the dictionary is of type 'MvcApplication.Areas.Admin.Controllers.CityController+d__1', but this dictionary requires a model item of type 'MvcApplication.Areas.Admin.Models.CityDto'.

image for debug info for what is being returned

,但我不明白這是什麼意思....

System.Collections.Generic.IEnumerator<MvcApplication.Areas.Admin.Models.CityDto>.Current == null 

System.Collections.IEnumerator.Current == null 

ds == null 

但結果視圖顯示了預期的數據

+0

你'_CityDto'永遠不會初始化。 –

+0

我應該在哪裏初始化它? –

+0

我不知道。你想用它做什麼? –

回答

0

我不得不進口IEnumerable的模型考慮過 升IEnumerable<MvcApplication.Areas.Admin.Models.CityDto>

不喜歡<MvcApplication.Areas.Admin.Models.CityDto>

而且我用這個在控制器指數HTTPGET阿克頓

var model = obj.Details().Tables[0].AsEnumerable().Select(t => new CityDto {City_id=t.Field<long>("city_id"), 
     State_id=t.Field<long>("state_id"), 
     State_name=t.Field<string>("state_name"), 
     City_name=t.Field<string>("city_name") 
     }); 
     //var model = GetCities(obj.Details()).AsEnumerable().Select(t => t); 
     return View(model);