2010-05-01 2 views
0

내장 된 AccountModel에 역할 공급자를 추가하고 있지만 등록보기 모델을 사용하여 내보기에 GetAllRoles를 추가하는 데 문제가 있습니다. AccountModel역할 공급자 - AccountModel

public class RegisterModel 
    { 
     UserName, Email Etc.... 

     [Required] 
     [DisplayName("AllRoles")] 
     public SelectList AllRoles { get; set; } 
    } 

역할 서비스에서

보기 모델

 Form... <div class="editor-label"> <%= Html.LabelFor(m => m.ConfirmPassword) %> </div> <div class="editor-field"> <%= Html.PasswordFor(m => m.ConfirmPassword) %> <%= Html.ValidationMessageFor(m => m.ConfirmPassword) %> </div> <%= Html.DropDownListFor(m => m.AllRoles)%> 
RegisterModel

AccountModel

public interface IRolesService 
{ 
    SelectList GetAllRoles(); 
} 

    public class RolesService : IRolesService 
{ 
    public SelectList GetAllRoles() 
    { 
     var AllRoles = new SelectList(Roles.GetAllRoles()); 
     return AllRoles; 
    } 
} 

등록보기 페이지 상속에 추가

보기 모델의 모든 역할을 사용하여 드롭 다운 목록을 채우는 방법을 모르겠습니다.

도움이 될 것입니다!

+0

이렇게하면됩니다. 오류가 무엇입니까 점점. – Amitabh

+0

아래 게시물에 내 코드를 추가하고 오류가 발생했습니다 ... 인스턴스를 만들 때 New 키워드를 사용하십시오. – Jemes

답변

0

선택한 역할과 전체 역할 목록에 대한 속성이 필요하다고 생각합니다. 역할 목록이 드롭 다운을 채우기 위해 사용되며 선택한 역할은 선택한 값으로 게시물에 채워집니다.

public class RegisterModel 
{ 
    UserName, Email Etc.... 

    [Required] 
    [DisplayName("Role")] 
    public string Role { get; set; } 

    [ScaffoldColumn(false)] 
    public SelectList AllRoles { get; set; } 
} 

... 
public ActionResult Register() 
{ 
    var roleService = new RoleService(); 
    var model = new RegisterModel 
    { 
      AllRoles = roleService.GetAllRoles(), 
      // Role = "User" if you want to choose a default 
    } 

    return View(model); 
} 


<div class="editor-label"> 
    <%= Html.LabelFor(m => m.ConfirmPassword) %> 
</div> 
<div class="editor-field"> 
    <%= Html.PasswordFor(m => m.ConfirmPassword) %> 
    <%= Html.ValidationMessageFor(m => m.ConfirmPassword) %> 
</div> 

<%= Html.DropDownListFor(m => m.Role, Model.AllRoles, "--select--", null)%> 
+0

HTML.DropDownListFor 코드에 대한 내보기에서 다음 오류가 발생하지만 이해가됩니다. New 키워드를 사용하여 인스턴스를 만듭니다 – Jemes

+0

@Jemes -'--select-- ", null'없이 사용하고 오류가 계속 발생하는지 확인하십시오. – tvanfosson

+0

작업이 여전히 똑같은 오류가 발생했습니다. – Jemes

관련 문제