繁体   English   中英

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

[英]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中读取List数据,但是我收到的错误是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)})

UserStatus

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
                           }) })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM