Search code examples
c#asp.netasp.net-mvcdropdownlistfor

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


I have editor template for 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" })

And I am reading List data from View, using ViewData, but I am getting error that System.String doesn't have property 'P' (that is my status). I think this is happening because when data come to my template, he is reading only value, not property. Is there any workaround for this?

View

@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 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;
        }
    }
}

Solution

  • So, as @vikscool stated, I fixed this problem by using (IEnumerable<SelectListItem>)ViewData["selectList"] in my template, and in view, I am selecting item like this:

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