更新 2,
工作了几个小时后,我注意到,默认情况下,VS 2010 会生成一个模板,该模板在 Create 方法中填充,其中包含员工 ID 和单个姓名......
所以,
创建方法:
- 添加 ViewBag.employeeid 和(ids,复合名称)列表
创建视图:
就这样。以前没有看到这么明显的东西,我觉得很愚蠢...
你怎么看,戴夫A?
编辑
只需添加用于构建列表的代码和复合名称:
--> Employee html helper, 生成 SelectList (with selected item)
public static class employeeHelper
{
public static IEnumerable<SelectListItem> toSelectListItems(this IEnumerable<employee> Employees, Nullable<Int32> selectedID)
{
List<SelectListItem> lEmployees = new List<SelectListItem>();
foreach (employee ee in Employees)
lEmployees.Add(new SelectListItem
{
Selected = selectedID != null && ee.id == selectedID.Value,
Text = employeeExtender.getEmployeeNames(pp),
Value = pp.id.ToString()
});
lEmployees = lEmployees.OrderBy(p => p.Text).ToList();
return lEmployees;
}
}
--> Employee 扩展器,获取复合名称
使用 Stringbuilder 提高性能
public class employeeExtender
{
public static String getEmployeeNames(employee e)
{
if (e == null)
return null;
StringBuilder sb = new StringBuilder();
sb.Append(e.FamilyName.ToLower());
if (!String.IsNullOrEmpty(e.SecondFamilyName) && e.SecondFamilyName.Trim() != String.Empty)
sb.Append(" " + e.SecondFamilyName.ToLower());
sb.Append(", ");
sb.Append(e.Name.ToLower());
//Capitalize String
return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(sb.ToString());
}
}