1

I have a table in my database which I select all Mem_NA to fill it in my dropdownlist in my view.

I can't understand how can I fill the values in there? Someone can give me a hand?

My Model

public class MemberBasicData
{
    public int Id { get; set; }
    public string Mem_NA { get; set; }
    public string Mem_Occ { get; set; }
}

 public List<MemberBasicData> GetAllMembers()
 {
   DateTime today = DateTime.Now;
   List<MemberBasicData> mbd = new List<MemberBasicData>();
   using (SqlConnection con = new SqlConnection(Config.ConnectionString))
   {
     using (SqlCommand cmd = new SqlCommand("SELECT Id,Mem_NA,Mem_Occ FROM Mem_Basic", con))
     {
       try
       {
         con.Open();
         SqlDataReader reader = cmd.ExecuteReader();
         while (reader.Read())
         {
           MemberBasicData mb = new MemberBasicData();
           mb.Id = (int)reader["Id"];
           mb.Mem_NA = (string)reader["Mem_NA"];
           mb.Mem_Occ =(string)reader["Mem_Occ"];
           mbd.Add(mb);
         }
       }
       catch (Exception e) 
       { 
         throw e; 
       }
       finally 
       { 
         if (con.State == System.Data.ConnectionState.Open) 
         { 
           con.Close(); 
         }
       }

       return mbd;
     }
   }
 }

GetAllMembers() function is an IEnumerable function which I was used for get all details of members. Can I use the same function for filling the dropdown list?

My controller

public ActionResult Register()
{                              
  return View(new Member());
}

And View

@Html.DropDownListFor("-- Select --", new SelectList("")) <=== ???? i dont know
Manuel Rauber
  • 1,583
  • 1
  • 17
  • 39
neel
  • 5,123
  • 12
  • 47
  • 67
  • Btw. you should not use `catch (...) { throw e; } `. Use this only if you want to change the source and stacktrace where the exception occurs: http://stackoverflow.com/a/1697241/959687 – Manuel Rauber Aug 26 '13 at 06:03

2 Answers2

1

Model

public MemberModel
{
    List<SelectListItem> MemberList { get; set; }
}

Controller

public ActionResult Register()
{
    MemberModel model = new MemberModel();

    model.MemberList = GetAllMembers()
        .Select(m => new SelectListItem
        {
            Text = string.format("{0}, {1}", m.Mem_NA, m.Mem_Occ),
            Value = m.Id
        });

    SelectListItem default = new SelectListItem
    {
        Text = "-- SELECT --",
        Value = 0
    };

    model.MemberList.Insert(0, default);

    return View(model);
}

View

@Html.DropDownList(Model.MemberList);

UPDATE: This version adds a default entry to the beginning of the list.

Neil T.
  • 3,308
  • 1
  • 25
  • 30
0

@Html.DropDownListFor gets an IEnumerable of SelectListItem. But instead, you want to use a list of MemberBasicData. You must convert your MemberBasicData list to SelectListItem and pass it to the @Html.DropDownListFor instead of a list of MemberBasicData. For passing related data to the Views as Sirwan pointed, you can use ViewBag or ViewData.

Jaimin
  • 7,964
  • 2
  • 25
  • 32
Siyamand
  • 54
  • 2