2012-07-11 3 views
0

(데이터를 생성하고 업데이트하는)이 코드가 (당신이 사용자 프로필을 편집 할 때하드 코딩 드롭 다운

이 (해당 CRUD와 (Controlers + 조회수)) 당신이 SexType에 대한 드롭 다운을 볼 수 있습니다 당신이 다른 CRUD로도 수정할 수 있음) SexType 테이블에 해당합니다.

public class UserProfile{ 
public int Id { get; set; } 
public string Name { get; set; } 
public string Surname { get; set; } 
public int SexTypeId { get; set; } 
public virtual SexType SexType { get; set; } 

public class SexType 
{ 
    public int SexTypeId { get; set; } 
    public string SexTypeDescription { get; set; } 
    public virtual ICollection<UserProfile> UserProfiles { get; set; } 
} 

public class UserProfileDBContext : DbContext //This creates the database according to the model 
{ 
    public DbSet<UserProfile> UserProfiles { get; set; } 
    public DbSet<SexType> SexType { get; set; } 
} 

내가 원하는 것은 : 나는 간단한 일을 원하지만 내가 그것을 할 관리 할 수 ​​없습니다 :

내가있는 sextype 내 모델 (남성/여성) 하드 코딩 할 (또는 제어 장치). 데이터베이스에 없습니다. "UserProfile"에서 레코드를 만들거나 업데이트 할 때마다 데이터베이스가 필요 없지만 "male/female"옵션을 사용하여 드롭 다운을 시각화합니다.

도와 주실 수 있습니까?

+0

cshtml은 어떻게 생겼습니까? –

+0

성별 대신 성별을 묻는 이유는 무엇입니까? – CodesInChaos

답변

2

당신이보기에이 작업을 수행 할 수 있습니다

@Html.DropDownList("SexType", 
    new[] { 
     new SelectListItem() { Text="Male", Value = "M" }, 
     new SelectListItem() { Text="Female", Value = "F" } 
    } 
); 
+0

또한 "지정하지 않음/선호하지 않음"옵션과 "기타"옵션을 추가해야합니다. – CodesInChaos

+0

"둘 다"는 어떻습니까? –

+0

다른 카테고리에 포함 시키겠습니다. – CodesInChaos

0

이 목록을 해주 SexTypes

public class UserProfile{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Surname { get; set; } 
    public int SexTypeId { get; set; } 
    public IEnumerable<SelectListItem> SexTypes { get; set; } 
} 

그리고 당신의 GET의 조치 방법의를 개최하여 UserProfile 뷰 모델에 새 속성을 추가, 당신은 채울 수 있습니다 그것을보고보기로 돌아 가기

public ActionResult Create() 
{ 
    var model=new UserProfile(); 
    model.SexTypes = new[] 
    { 
    new SelectListItem { Value = "M", Text = "Male" }, 
    new SelectListItem { Value = "F", Text = "FeMale" },   
    }; 
    return View(model); 
} 

그리고 r보기가 강력하게 바운딩 ViewModel,

@Html.DropDownListFor(x => x.SexTypeId, new SelectList(Model.SexTypes, 
                "Value","Text"), "Select..") 
+0

답장을 보내 주셔서 감사합니다. 나는 그것을 최대한 빨리 시도 할 것이다. 나는 단지 레코드 편집을위한 추가 코드를 작성해야 할 것이라고 생각하고있다 ... – PnP

관련 문제