how to add option value in dropdown/ default dropdown value
var EmpPreviousChart = (from m in dataContext.ViewName
where ((m.EmployeeID == Id)&&(m.Status=='C'))
orderby m.EmployeeID descending
select m.EmployeeID + "- " + m.JoinDate.ToString());
ViewData["EmpPreviousChartList"] = EmpPreviousChart;
The above code I am using to retrieve data using linq.
<%= Html.DropDownList("EmpPreviousCharts", new SelectList((IEnumerable)ViewData["EmpPreviousChartList"]), "Select", new { styl开发者_如何学编程e = "width:60px;font-family:Verdana;font-size:9px;", ID = "EmpPrevCharts" })%>
the above code to fill my dropdown, I m getting select in very first place. Now I want that the value that should be visible after loading the dropdown should be next to select. ie If I had select,A,B,C resp. I want A to be visible. I m using partialview for this. If I could add an autoincrement field in option value I can achive it or if there is any other way out
SelectList
takes the currently selected item as one of the parameters - just pass the item you want to be selected.
You should be instantiating SelectList
in your controller, and then pass it to the view. The view should have as little logic as possible.
var EmpPreviousChart = (from m in dataContext.ViewName
where ((m.EmployeeID == Id)&&(m.Status=='C'))
orderby m.EmployeeID descending
select m.EmployeeID + "- " + m.JoinDate.ToString()).ToList();
ViewData["EmpPreviousChartList"] = new SelectList(EmpPreviousChart, EmpPreviousChart.FirstOrDefault());
<%= Html.DropDownList("EmpPreviousCharts", (SelectList)ViewData["EmpPreviousChartList"], "Select", new { style = "width:60px;font-family:Verdana;font-size:9px;", ID = "EmpPrevCharts" })%>
精彩评论