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);
}
他們都返回一個'字典' –
SentOnLine
我現在看你的例子。我假設你沒有收到任何編譯錯誤?您正在獲取的錯誤意味着您正試圖訪問字符串上的「Key」屬性。 我會仔細檢查您的存儲庫的返回值,並從那裏調整。對於有類似問題的人,請參閱此答案:https://stackoverflow.com/questions/6705488/dropdownlistfor-throws-an-error#answer-6708179 – Nick