Domanda

I want to add month and year data into drop down list from the database. but I'm getting an error. so any help can be appriciated.

This is my view:

 @using (Html.BeginForm("ViewDataOfDatabase", "AirtelManagement",FormMethod.Post))
 {
    <p><h3>Find by PhoneNumber:@Html.TextBox("SearchString",ViewBag.CurrentFilter as    string)</h3></p>

    <p>Year:@Html.DropDownListFor(x => x.YearSelectedId, new SelectList(Model.YearList))
    Month:@Html.DropDownListFor(x=>x.MonthSelectedId,new SelectList(Model.MonthList))</p>

    <p><input type="submit" value="Search" /></p>

this is my model.

public class AirtelManagementModel
{
    public long MobileAcNumber { get; set; } 
    public long AirtelNumber { get; set; }
    public int OneTime { get; set; }
    public float MonthlyCharges { get; set; }
    public float CallCharges { get; set; }
    public float ValueAddedServices { get; set; } 
    public float MobileInternetUsage { get; set; } 
    public float Roaming{ get; set; }
    public float Discounts { get; set; }
    public float Taxes { get; set; }
    public float TotalCharges { get; set; }
    public string WhoUploaded { get; set; }
    public DateTime UploadedDate { get; set; }
    public DateTime DateOfCreation { get; set; }
    public int ImportDateId { get; set; }

    public int MonthSelectedId { get; set; }

    public List<AirtelManagementModel> MonthList
    {
        get;
        set;
    }

    public int YearSelectedId { get; set; }

    public List<AirtelManagementModel> YearList
    {
        get;
        set;
    }
}

this is repository class' method through which i'm fetching the data from database

     public List<AirtelManagementModel> GetYearFromImportDate()
    { 

        Database _db=DatabaseFactory.CreateDatabase("CCIDBConnection");
        IDataReader _reader = _db.ExecuteReader("[dbo].GetYearFromDates");
        List<AirtelManagementModel> _list = new List<AirtelManagementModel>();
        while(_reader.Read())
        {
            AirtelManagementModel _model = new AirtelManagementModel();
            _model.YearSelectedId = Convert.ToInt32(_reader["Id"]);
            _model.YearList = (List<AirtelManagementModel>)_reader["YearOfDate"];
            _list.Add(_model);
        }
        _reader.Close();
        return _list;
    }
È stato utile?

Soluzione

Try to use a List of SelectListItems instead of the SelectList!

  public List<SelectListItem> MonthList
  {
    get;
    set;
  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top