답장을 보내 주셔서 감사합니다. 실제로 View가 아닌 ViewModel을 사용하여 문제를 해결할 수있었습니다. 다른 한편으로, 일부 연구 후에 ViewModel을 사용하지 않고도 Dropdownlist를 채우기 위해 다른 효과적인 방법을 적용했습니다. 또한이 예제를 사용하면 아래와 같이 동일한 조회 테이블에서 여러 개의 외래 키를 사용할 수 있습니다. 신청자 3 개의 외래 키와 Lookup 이러한 키와 관련된 엔티티가 있습니다. 이 예제를 사용하여 달성하고자하는 것은 정확히 여러 Dropdownlist 매개 변수 즉, 성별, 예/아니요, 상태 등에 대한 조회 테이블을 사용하는 것입니다. 여러 매개 변수에 대한 테이블을 만들 필요가 없으므로 이러한 매개 변수는 구분됩니다 (이 매개 변수는 구별됩니다) 찾아보기 테이블의 LookupType 속성).
신청자 법인 :
public class Applicant
{
[Key]
public int ApplicantID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
// for using "Multiple foreign keys within same table using Fluent API"
public int? HasDoneAnyProject { get; set; }
public int? IsInterestedAnyProgramme { get; set; }
public int? InterestedProgrammeId { get; set; }
public virtual Lookup PrimaryLookup { get; set; }
public virtual Lookup SecondaryLookup { get; set; }
public virtual Lookup TertiaryLookup { get; set; }
}
조회 엔터티 : 여기에 아래의 전체 예제는 (내가 간결 관련이없는 속성을 단락 한)입니다
public class Lookup
{
[Key]
public int LookupID { get; set; }
public string LookupType { get; set; }
public string LookupValue { get; set; }
// for using "Multiple foreign keys within same table using Fluent API"
public virtual ICollection<Applicant> PrimaryLookupFor { get; set; }
public virtual ICollection<Applicant> SecondaryLookupFor { get; set; }
public virtual ICollection<Applicant> TertiaryLookupFor { get; set; }
}
DbContext :
public class EFDbContext : DbContext
{
public DbSet<Applicant> Applicants { get; set; }
public DbSet<Lookup> Lookups { get; set; }
//for using "Multiple foreign keys within same table using Fluent API"
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Applicant>()
.HasOptional(b => b.PrimaryLookup)
.WithMany(a => a.PrimaryLookupFor)
.HasForeignKey(b => b.HasDoneAnyProject)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Applicant>()
.HasOptional(b => b.SecondaryLookup)
.WithMany(a => a.SecondaryLookupFor)
.HasForeignKey(b => b.IsInterestedAnyProgramme)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Applicant>()
.HasOptional(b => b.TertiaryLookup)
.WithMany(a => a.TertiaryLookupFor)
.HasForeignKey(b => b.InterestedProgrammeId)
.WillCascadeOnDelete(false);
}
}
컨트롤러 :
private void PopulateLookupsDropDownList(string lookupType, string foreignKey, object selectedLookups = null)
{
var lookupsQuery = repository.Lookups
.Select(x => x)
.Where(x => x.LookupType == lookupType)
.OrderBy(x => x.ParentLookupID).ToList();
ViewData[foreignKey] = new SelectList(lookupsQuery, "LookupID", "LookupValue", selectedLookups);
}
세 DropDownList로 각각의 방법을 호출하기위한 :
PopulateLookupsDropDownList("YesNo", "HasDoneAnyProject", applicant.HasDoneAnyProject);
PopulateLookupsDropDownList("YesNo", "IsInterestedAnyProgramme", applicant.IsInterestedAnyProgramme);
PopulateLookupsDropDownList("Programme", "InterestedProgrammeId", applicant.InterestedProgrammeId);
보기 : :
<label>Has done any project before?</label>
@Html.DropDownList("HasDoneAnyProject", "---- Select ----")
<label>Are you interested in any programme?</label>
@Html.DropDownList("IsInterestedAnyProgramme", "---- Select ----")
<label>Interested programme name?</label>
@Html.DropDownList("InterestedProgrammeId", "---- Select ----")
: 다른 LookupType 매개 변수와 같은 조회 테이블에서 세 개의 드롭 다운리스트의 각을 채우기
이 접근 방식이 유용하게 사용되기를 바랍니다. 드롭 다운 목록을 동일한 조회 테이블에서 채우려는 사람들을위한 것입니다. 다른 한편으로는, 이것에 적합 할뿐만 아니라, 다른 테이블의 드롭 다운리스트를 채우는데도 사용할 수 있습니다.
감사합니다.
'문제가 발생하지 않았습니다 ...'product.SelectedLookupId'의 문제점은 무엇입니까? – AminSaghi