Click here to Skip to main content
14,113,788 members
Rate this:
 
Please Sign up or sign in to vote.
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...

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;
    }


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

    public SelectList GenderList { get; set; }
}



 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Umm, use this:
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 13 Nov 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100