how to select the listitem value of dropdownlistfor in asp.net MVC2
===

> this is the Model of userlogin.cs and it holdes the properties and it get the data from database


 public class UserLogin
    {
        public string strConn;
        public SqlConnection DBConn;
        public SqlCommand cmd;
        public SqlDataAdapter adptr;                

        [Required(ErrorMessage="Employee Number Is Requierd")]        
        [DisplayName("EmployeeNumber")]
        public int EmployeeNumber { get; set; }

        [Required(ErrorMessage="First Name Is Requierd")]
        [DataType(DataType.Text)]
        [DisplayName("FirstName")]
        public string FirstName { get; set; }

        [Required(ErrorMessage="Last Name Is Requierd")]   
        [DisplayName("LastName")]
        public string LastName { get; set; }

        [Required(ErrorMessage="Title Field Is Requierd..")]   
        [DisplayName("Title")]
        public string Title { get; set; }

        [Required(ErrorMessage="Hourly Salary Is Requierd")]   
        [DisplayName("HourlySalry")]
        public string HourlySalry { get; set; }

        [Required(ErrorMessage="Mention Department Code")]   
        [DisplayName("DepartmentCode")]
        public string DepartmentCode { get; set; }

        [Required(ErrorMessage = "Select Department Name")]
        [DisplayName("DepartmentName")]
        public IEnumerable<tbl_selecteditem> Department { get; set; }

        public UserLogin()
        {
            DataSet ds = BindDepartment();
            List<tbl_selecteditem> Departmentlist = new List<tbl_selecteditem>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int j = 0, k = 1;                
                tbl_selecteditem sli = new tbl_selecteditem();
                sli.Code = ds.Tables[0].Rows[i][j].ToString();
                sli.Name = ds.Tables[0].Rows[i][k].ToString();
                //sli.Value = Code;
                //sli.Text = Name;
                Departmentlist.Add(sli);
            }
            Department = Departmentlist.AsEnumerable();
        }
        public DataSet BindDepartment()
        {
            try
            {
                strConn =      ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
                DBConn = new SqlConnection(strConn);
                DBConn.Open();
                string Cmdtext = "Select * from Departments";
                cmd = new SqlCommand(Cmdtext, DBConn);
                adptr = new SqlDataAdapter();
                adptr.SelectCommand = cmd;
                DataSet ds = new DataSet();
                adptr.Fill(ds);
                return ds;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Dispose(); DBConn.Close(); }
        }
    }
    public class tbl_selecteditem
    {
        [Required]
        [DisplayName("Code")]
        public string Code { get; set; }

        [Required]
        [DisplayName("Name")]
        public string Name { get; set; }
    }

> UserloginController.cs is controller class that use the Userlogin.cs model for create the view User.aspx.

 public class UserLoginController : Controller
    {                 
        //
        // GET: /UserLogin/

        public ActionResult User()
        {
           Models.UserLogin objmodel = new Models.UserLogin();                        
           return View(objmodel);
        }

        [HttpPost]
        public ActionResult User(Models.UserLogin model)
        {            
            IEnumerable<string> str=model.Department.Select(c => c.Name);                        
            if (ModelState.IsValid == true)
            {
                EmployeeDAL dal = new EmployeeDAL();
                int i = dal.InserNewEmployee(model);
            }
            else { return View(model); }
            return RedirectToAction("User");
        }
}

> User.aspx is the view file and in which i used dropdownlistfor control that bind the listitem from the userlogin.cs model of Department property.But its does not post the selected value.

    <asp:Content ID="UserMainContent" ContentPlaceHolderID="MainContent" runat="server">

    <h2>User</h2>
    <% using (Html.BeginForm())
       { %>
        <%: Html.ValidationSummary(true, "Fields Is Requierd. Please correct the errors and try again.")%>
        <table style="border: solid 0px #E8EEF4">
        <tr>
        <td style="border: solid 0px #E8EEF4">
            <fieldset style="width:0">
                <legend>Create New Employee</legend>
                <div class="editor-label">
                <%: Html.LabelFor(m=>m.EmployeeNumber)%>
                </div>
                <div class="editor-label">
                <%:Html.TextBoxFor(m=>m.EmployeeNumber) %>
                <%:Html.ValidationMessageFor(m=>m.EmployeeNumber) %>
                </div>
                <div class="editor-label">
                <%:Html.LabelFor(m=>m.FirstName) %>
                </div>
                <div class="editor-label">
                <%:Html.TextBoxFor(m => m.FirstName)%>
                <%:Html.ValidationMessageFor(m => m.FirstName)%>
                </div>
                <div class="editor-label">
                <%:Html.LabelFor(m=>m.LastName) %>
                </div>
                <div class="editor-label">
                <%:Html.TextBoxFor(m => m.LastName)%>
                <%:Html.ValidationMessageFor(m => m.LastName)%>
                </div>
                <div class="editor-label">
                <%:Html.LabelFor(m=>m.Title) %>
                </div>
                <div class="editor-label">
                <%:Html.TextBoxFor(m => m.Title)%>
                <%:Html.ValidationMessageFor(m => m.Title)%>
                </div>
                <div class="editor-label">
                <%:Html.LabelFor(m=>m.HourlySalry) %>
                </div>
                <div class="editor-label">
                <%:Html.TextBoxFor(m => m.HourlySalry)%>
                <%:Html.ValidationMessageFor(m => m.HourlySalry)%>
                </div>
                 <div class="editor-label">
                <%:Html.LabelFor(m=>m.DepartmentCode) %>
                </div>
                <div class="editor-label">
                <%:Html.TextBoxFor(m => m.DepartmentCode)%>
                <%:Html.ValidationMessageFor(m => m.DepartmentCode)%>                               
                </div>
                <div class="editor-label">
                <table style="border: solid 0px #E8EEF4">
                <tr>
                <td style="border: 0px"><%:Html.LabelFor(m=>m.Department) %></td>
                <td style="border: 0px"><%:Html.DropDownListFor(m => m.Department, Model.Department.Select(c => new SelectListItem { Text = c.Name, Value = c.Code.ToString() }), "--Select Department--")%><%--<%= Html.DropDownList("Numbers",(IEnumerable<SelectListItem>)ViewData["Numbers"],"--Select Department--") %>--%></td>                
                </tr>
                </table> 
                </div>
                <div class="editor-label">
                 <input type="submit" value="User" style="background-image:url(../../Content/cancel_button_.png)" />
                 </div>
                </fieldset>
                </td>
                <td style="border: solid 0px #E8EEF4" height="67px"><img src="../../Content/typing%20on%20computer%20(Small).jpg" /></td>
                </tr>            
                </table>
                <%} %>

</asp:Content>
