2017-06-14 117 views
0

我试图重新创建答案中的解释this SO回答 但我得到这个错误并没有任何意义,因为我做了到底什么答案说...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> 

视图模型

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()第三参数值和文本字符串名称字段。 IDC.GetAvailableCustomerNumbers(recCenID)是否返回Dictionary<string, string>?您引用的示例在其代码中使用了字典,这就是“Key,Values”字符串工作的原因。如果您使用的是IEnumerable<T>,请尝试使用模型上的属性名称而不是“Key”和“Value”。

+0

他们都返回一个'字典' – SentOnLine

+1

我现在看你的例子。我假设你没有收到任何编译错误?您正在获取的错误意味着您正试图访问字符串上的“Key”属性。 我会仔细检查您的存储库的返回值,并从那里调整。对于有类似问题的人,请参阅此答案:https://stackoverflow.com/questions/6705488/dropdownlistfor-throws-an-error#answer-6708179 – Nick

相关问题