[英]Editor template for DropDownList, can't bind property from View to template
I have editor template for DropDownList 我有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). 我正在使用ViewData从View中读取List数据,但是我收到的错误是
System.String
没有属性'P'(这是我的状态)。 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 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;
}
}
}
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: 所以,正如@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.