我具有 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;
}
}
}
答案 0 :(得分:0)
因此,正如@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
}) })