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
1
  • Btw. you should not use catch (...) { throw e; } . Use this only if you want to change the source and stacktrace where the exception occurs: stackoverflow.com/a/1697241/959687 Commented Aug 26, 2013 at 6:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.