首先,我知道下拉列表应该在各自的 ViewModel 中分开,最好在局部视图中发出。
但是,如果您可以帮助我解决以下问题,这将告诉我为什么我无法在设计时使用我的超类中的两个类进行编译(因为我需要它们在同一个视图中。
如果我有一个如下所述的超类:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using YeagerTechModel.DropDownLists;
namespace YeagerTechModel.ViewModels
{
[DataContract]
[Serializable]
public partial class CustomerProjectDDL
{
[DataMember]
public Customer Customer = new Customer();
[DataMember]
public ProjectName ProjectName = new ProjectName();
}
}
ProjectName 类的定义如下:
namespace YeagerTechModel.DropDownLists
{
[DataContract]
[Serializable]
public partial class ProjectName
{
[DataMember]
public Int16 ProjectID { get; set; }
[DataMember]
public String Name { get; set; }
}
}
当我尝试使用上述方法时,在我的方法中出现以下设计时编译错误:新语句之后的左花括号:“无法使用集合初始化程序初始化类型'YeagertechModel.ViewModels.CustomerProjectDDL',因为它没有实施 IENumerable。”
public List<CustomerProjectDDL> GetProjectNameDropDownListVM()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
var project = DbContext.Projects.Where(w => w.ProjectID > 0).Select(s =>
new CustomerProjectDDL()
{
ProjectName.ProjectID = s.ProjectID,
ProjectName.Name = s.Name
});
List<CustomerProjectDDL> myProjects = new List<CustomerProjectDDL>();
myProjects = project.ToList();
return myProjects;
}
}
catch (Exception ex)
{
throw ex;
}
}