Click here to Skip to main content
14,883,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to populate the dropdownlistfor form database but this error is arises... Can Someone help me with this to remove this error Cannot implicitly convert type 'ORS.DTO.GenderDTO' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

i have to populate dropdownlist for Gender and have to get values that are stores in the database...

XML
public IEnumerable<GenderDTO> GetGenderList()
    {
        GenderDTO model = new GenderDTO();
        var gender = from g in db.CAT_GENDER
                     select g;
        model.GenderList = new SelectList(gender, "ID", "Description");
        return model;
    }


C#
public class GenderDTO
{
    public int ID { get; set; }
    public string Description { get; set; }

    public SelectList GenderList { get; set; }
}



C#
 public partial class CAT_GENDER
{
    public CAT_GENDER()
    {
        USER_PROFILE = new HashSet<USER_PROFILE>();
    }

    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    public string DESCRIPTION { get; set; }

    public int STATUS_ID { get; set; }

    public virtual CAT_RECORD_STATUS CAT_RECORD_STATUS { get; set; }

    public virtual ICollection<USER_PROFILE> USER_PROFILE { get; set; }
}
Posted

Umm, use this:
C#
model.GenderList = db.CAT_GENDER.ToList().Select(g => new SelectListItem { Text = g.Description, Value = g.ID });


-KR
   
Comments
Abhinav S 13-Nov-15 11:22am
   
Oops. The return type of the method does not match the type returned. This this solution will still not resolve the problem.
Method GetGenderList() has a return type of IEnumerable<genderdto></genderdto>.
However, model (value that is returned) is of type GenderDTO.

Use the following code instead
GenderDTO model = new GenderDTO();
       var gender = from g in db.CAT_GENDER
                    select g;
       model.GenderList = new SelectList(gender, "ID", "Description");
       List<genderdto> itms = new List<genderdto> ();
       itms.Add(model);
       return itms;</genderdto></genderdto>
   
v2
Comments
Member 10740412 13-Nov-15 11:31am
   
By using this i am getting this exception.. can you help to solve this...
Cannot convert type 'System.Collections.Generic.List<ors.dto.genderdto>' to 'System.Web.Mvc.SelectList'

Getting error at this line...
@Html.DropDownListFor(model => model.GenderID, (SelectList)ViewBag.Gender, new { @class = "form-contol" })
Abhinav S 13-Nov-15 11:34am
   
You cannot convert List view to SelectList implicitly.
Member 10740412 13-Nov-15 11:36am
   
Then what else i can do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900