System.String'不包含名称为' Key'

时间:2017-06-14 21:44:01

标签: asp.net asp.net-mvc asp.net-mvc-5

我正在尝试重新创建答案this SO答案中解释的内容 但我得到的这个错误并没有任何意义,因为我完全按照答案说的做了...

我应该说我对此比较陌生,所以任何帮助都非常感谢。谢谢 !

在视图中抛出错误的块

<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 个答案:

答案 0 :(得分:1)

SelectList()的第2和第3个参数分别是值和文本字段的字符串名称。 IDC.GetAvailableCustomerNumbers(recCenID)会返回Dictionary<string, string>吗?您引用的示例在其代码中使用了Dictionary,这就是“Key,Values”字符串工作的原因。如果您使用的是IEnumerable<T>,请尝试使用模型上的属性名称而不是“键”和“值”。