i can view anybody record and then can edit it if i click edit button below record, after clicking it takes you to another page whihc shows textboxes and dropdown, checkboxes to edit and all textboxes contains values for that EmpID but problem is that i want pre selected value in DROPDOWN LIST, like it should pick corresponding value for each user a preselected like my textboxes. I am using LINQ TO SQL, MVC 3, asp.net C#

Controller:

var DepNames = (from n in DataContext.HrDepts select new { n.DeptID, n.DeptName }).Distinct();
               ViewData["DeptName"] = new SelectList(DepNames, "DeptID", "DeptName", EmployeeValues.FirstOrDefault().DeptName);

View:

@using EmployeeAttendance_app.Models
@model IEnumerable<GetEmployeeEditDetails_SpResult>

@{
    var Item = Model.FirstOrDefault();
 }
@Html.DropDownList("DeptID", (SelectList)ViewData["DeptName"])

It is better to use the "dropdownlist.selected" attribute in the backend at page_Load event to the particular value which you want it to be selected.

do something like...

DropdownList1.SelectedIndex = DropdownList1.Items.IndexOf(DropdownList1.Items.FindByText(someTextvalue))

do something like...

DropdownList1.SelectedIndex =    
   DropdownList1.Items.IndexOf(DropdownList1.Items.FindByValue(somevalue))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.