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'.
,但我不明白這是什麼意思....
System.Collections.Generic.IEnumerator<MvcApplication.Areas.Admin.Models.CityDto>.Current == null
和
System.Collections.IEnumerator.Current == null
也
ds == null
但結果視圖顯示了預期的數據
你'_CityDto'永遠不會初始化。 –
我應該在哪裏初始化它? –
我不知道。你想用它做什麼? –