DropDownList 的编辑器模板,无法将 属性 从视图绑定到模板

Editor template for DropDownList, can't bind property from View to template

我有 DropDownList

的编辑器模板
@model object
<div class='form-fields @ViewData["class"]'>
@Html.LabelFor(x => x, new { @class = "form-fields__label" })
@Html.DropDownListFor(x => x, (SelectList)ViewData["selectList"], new { @class = "form-fields__input width-max d-block " + @ViewData["inputClass"]})
@Html.ValidationMessageFor(x => x, "", new { @class = "form-fields__form-error" })

我正在使用 ViewData 从 View 读取列表数据,但出现 System.String 没有 属性 'P' 的错误(这是我的状态)。我认为发生这种情况是因为当数据进入我的模板时,他只读取值,而不是 属性。有什么解决方法吗?

查看

@Html.EditorFor(x => x.AdminForEdit.Status.Status, "DropDownList", new { selectList = new SelectList(MDC.Models.Accounts.UserStatus.GetStatuses(), Model.AdminForEdit.Status.Status, NinjectWebCommon.Dictionary.GetLocalization(Model.Device.Language.LanguageId, Model.AdminForEdit.Status.Name), Model.AdminForEdit.Status.Status)})

用户状态 class

public class UserStatus
{
    public string Status { get; set; }
    public string Name { get; set; }
    public bool AlowLogIn { get; set; }
    public string ErrorMessage { get; set; }

    public UserStatus()
    {

    }

    public UserStatus(string status, string name, bool alowLogin, string errorMessage)
    {
        Status = status;
        Name = name;
        AlowLogIn = alowLogin;
        ErrorMessage = errorMessage;
    }

    public static UserStatus Pending = new UserStatus("P", "L_USERSTATUS_PENDING", false, "L_ADMIN_LOGIN_ERRORUSERPENDING");

    public static UserStatus Active = new UserStatus("A", "L_USERSTATUS_ACTIVE", true, "L_ADMIN_LOGIN_WRONGCREDENTIALS");

    public static UserStatus Blocked = new UserStatus("B", "L_USERSTATUS_BLOCKED", false, "L_ADMIN_LOGIN_ERRORUSERBLOCKED");

    public static UserStatus Deleted = new UserStatus("D", "L_USERSTATUS_DELETED", false, "L_ADMIN_LOGIN_ERRORUSERBLOCKED");

    public static UserStatus Unknown = new UserStatus("X", "L_USERSTATUS_UNKNOWN", false, "L_ADMIN_LOGIN_ERRORUSERBLOCKED");

    public static List<UserStatus> GetStatuses()
    {
        return new List<UserStatus> { Pending, Active, Blocked, Deleted };
    }

    public static UserStatus GetStatus(string status)
    {
        switch (status)
        {
            case "P":
                return Pending;
            case "A":
                return Active;
            case "B":
                return Blocked;
            case "D":
                return Deleted;
            default:
                return Unknown;
        }
    }
}

所以,正如@vikscool 所说,我通过在模板中使用 (IEnumerable<SelectListItem>)ViewData["selectList"] 解决了这个问题, 在视图中,我正在选择这样的项目:

@Html.EditorFor(x => x.AdminForEdit.Status.Status, "DropDownList", new { selectList = MDC.Models.Accounts.UserStatus.GetStatuses().Select(x => new SelectListItem()
                           {
                                Text = NinjectWebCommon.Dictionary.GetLocalization(Model.Device.Language.LanguageId, x.Name),
                                Value = x.Status
                           }) })