2015-09-29 2 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); 
Смежные вопросы