2017-06-14 4 views
0

回答に記載されている内容を再作成しようとしていますthis答えが ですが、このエラーが発生しています。まさにその答えが言った...System.String 'に' Key 'という名前のプロパティが含まれていません

私はこれに比較的新しいので、すべての助けが大歓迎と言わなければならない。ありがとうございました !

ビュー

にエラーがスローブロック
<div class="form-group"> 
    @Html.LabelFor(m => m.CustomerNumber, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(m => m.CustomerNumber, Model.AvailableCustNum) 
    </div> 
</div> 

にViewModelに

public class RegisterViewModel 
{ 
    [Required] 
    [EmailAddress] 
    [Display(Name = "Email")] 
    public string Email { get; set; } 

    [Required] 
    [Display(Name = "User Name")] 
    [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)] 
    public string UserName { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    [DataType(DataType.PhoneNumber)] 
    [Display(Name = "Phone Number")] 
    public string PhoneNumber { get; set; } 

    [Required] 
    [Display(Name = "Customer Number")] 
    public string CustomerNumber { get; set; } 

    [Required] 
    [Display(Name = "DeptID")] 
    public int DeptID { get; set; } 

    [Required] 
    [Display(Name = "Department Name")] 
    public string DepartmentName { get; set; } 

    [Required] 
    [Display(Name = "Group")] 
    public string Group { get; set; } 

    [Required] 
    [Display(Name = "Address Line 1")] 
    public string AddressLine1 { get; set; } 

    [Display(Name = "Address Line 2")] 
    public string AddressLine2 { get; set; } 

    [Required] 
    [Display(Name = "City")] 
    public string City { get; set; } 

    [Required] 
    [Display(Name = "State")] 
    public string State { get; set; } 

    [Required] 
    [Display(Name = "Zip Code")] 
    public string Zip { get; set; } 

    [Required] 
    [Display(Name = "Country")] 
    public string Country { get; set; } 

    [Required] 
    [Display(Name = "UACC")] 
    public int UACC { get; set; } 

    [Required] 
    [Display(Name = "IsRecordCenter")] 
    public bool IsRecCenter { get; set; } 

    [Required] 
    [Display(Name = "IsCustAdmin")] 
    public bool IsCustAdmin { get; set; } 


    public SelectList AvailableGroups { get; set; } 

    public SelectList AvailableCustNum { get; set; } 

    public SelectList AvailableDept { get; set; } 


} 

コントローラ

public ActionResult Register() 
    { 
     RecordCenterLoginRepo RCLR = new RecordCenterLoginRepo(); 
     IdentityCheck IDC = new IdentityCheck(); 

     var recCenID = RCLR.GetRecordCenterID((string)Session["RecordCenter"]); 


     Dictionary<string,int> custNums = IDC.GetAvailableCustomerNumbers(recCenID); 
     Dictionary<string,int> depts = IDC.GetAvailableDepartments((string)Session["CustomerNumber"], recCenID); 
     Dictionary<string,int> groups = IDC.GetAvailableGroups((string)Session["CustomerNumber"], recCenID); 

     RegisterViewModel model = new RegisterViewModel() { 

      AvailableCustNum = new SelectList(custNums, "Key", "Value"), 
      AvailableDept = new SelectList(depts, "Key", "Value"), 
      AvailableGroups = new SelectList(groups, "Key", "Value") 
     }; 

     return View(model); 
    } 

答えて

1

SelectList()の2番目と3番目のパラメータは、値やテキストの文字列名ですフィールド。 IDC.GetAvailableCustomerNumbers(recCenID)Dictionary<string, string>を返しますか?引用した例では、コード内でDictionaryを使用しているため、「Key、Values」という文字列が機能します。 IEnumerable<T>を使用している場合、 "Key"と "Value"の代わりにモデルのプロパティ名を試してください。

+0

彼らはすべて 'Dictionary ' – SentOnLine

+1

を返します。私は今あなたの例でそれを見ています。私はそれでコンパイルエラーが発生していないと仮定していますか?取得しているエラーは、文字列の「Key」のプロパティにアクセスしようとしていることを意味します。 私はあなたのリポジトリの戻り値を確認し、そこから調整します。同様の問題を抱えている人には、この回答を参照してください:https://stackoverflow.com/questions/6705488/dropdownlistfor-throws-an-error#answer-6708179 – Nick

関連する問題