0

I'm trying to select data from my database to insert into my dropdown list. My problem is that the dropdown list shows nothing and I don't receive any errors. Can someone give a hand with this please?

Here is my code:

Model:

public int id { get; set; }
public string name{ get; set; }
public IEnumerable<SelectListItem> isto { get; set; }

 public ListagemEmpresas()
{
    isto = GetCompanies();
}

public SelectList GetCompanies()
{
    var list = new List<SelectListItem>();
    string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

    using (var con = new SqlConnection(connection))
    {
        con.Open();
        using (var command = new SqlCommand("SELECT * FROM Companieslist", con))
        {
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                string id = reader[0] as string;
                string name = reader[1] as string;
                list.Add(new SelectListItem() { Text = name, Value = id });
            }
        }
        con.Close();
    }
    return new SelectList(list, "Value", "Text");
}

public class DefaultConnection : DbContext
{
    public DbSet<CompanyList> abc{ get; set; }
}

Controller:

var dba = new DefaultConnection();
        var query = dba.abc.Select(c => new SelectListItem
        {
            Value = SqlFunctions.StringConvert((double)c.id),
            Text = c.name,

        });
        var model = new CompanyList
        {
            isto = query.AsEnumerable()
        };

        return View(model);

View:

@model MyProject.Models.CompanyList

@Html.DropDownListFor(m => m.name, Model.isto, "--Select One--")
9
  • You are not setting the name property for your company list. May be try setting that. Commented Aug 14, 2013 at 14:27
  • In your controller, you are only setting isto, but not Name Commented Aug 14, 2013 at 14:31
  • Post your Model completely, not just its properties. Looking at your code, it seems like the class CompanyList does not have a `nome_empresa' property that you are using. Commented Aug 14, 2013 at 14:31
  • @MaheshVelaga, SelectListItem does not have a Name property. Commented Aug 14, 2013 at 14:32
  • @ataravati I was referring to CompanyList not SelectListItem Commented Aug 14, 2013 at 14:34

1 Answer 1

2

Define your IEnumerable<SelectListItem> In your Controller like below. You need to convert the DbSet<CompanyList> to List, before you can use the ToString() method.

var query = dba.abc.ToList().Select(c => new SelectListItem
    {
        Value = c.id.ToString(),
        Text = c.name
    });

Then, in your View you will have (The DropDownList returns the Value of the selected item, not its Text):

@model MyProject.Models.CompanyList

@Html.DropDownListFor(m => m.id, Model.isto, "--Select One--")
Sign up to request clarification or add additional context in comments.

9 Comments

Error: Can not implicity type int to string
Sorry, my bad. You need to convert the id to string. Also, convert abc to List, in your linq query (that's for the ToString() method to work). See the updated answer.
i change my code with yours. thank you very much for this. but i can´t understand why i still not receive any data from my database.
So, you don't have a problem with the DropDownList, but with pooling data from database, right? What does dba.abc return?
return nothing.. im so confused.. im trying and trying a lot of things, but still not receive nothing... :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.