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
}
这是我的控制器代码:
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"])
};
}
}
没有任何构建错误,但是当我运行代码时,我收到错误:
传递到字典中的模型项的类型为&#39; MvcApplication.Areas.Admin.Controllers.CityController + d _1&#39;,但此字典需要类型为&#39; MvcApplication.Areas.Admin的模型项.Models.CityDto&#39;
但我不明白这意味着什么......
System.Collections.Generic.IEnumerator<MvcApplication.Areas.Admin.Models.CityDto>.Current == null
和
System.Collections.IEnumerator.Current == null
也
ds == null
但结果视图显示了预期的数据
答案 0 :(得分:0)
我也必须在视图中导入可数模型
升IEnumerable<MvcApplication.Areas.Admin.Models.CityDto>
不喜欢<MvcApplication.Areas.Admin.Models.CityDto>
我也在控制器索引中使用了这个httpget acton
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);