0
我創建它填充了枚舉值選擇列表驗證過程中的初始值:MVC3 Html.SelectList - 總是看到即使所選的選項已經改變
<%= Html.DropDownListFor(model => model.OrderStatus, new SelectList(Enum.GetValues(typeof(OrderStatus))))%>
我現在執行客戶端驗證,以確保該OrderStatus設置正確:
[DisplayName("Order Status"), EnsureOrderStatus("ID")]
public OrderStatus OrderStatus { get; set; }
public class EnsureOrderStatus : ValidationAttribute, IClientValidatable
{
private readonly string OrderIDPropertyName;
public EnsureOrderStatus(string orderIDPropertyName)
{
OrderIDPropertyName = orderIDPropertyName;
}
protected override ValidationResult IsValid (object value, ValidationContext validationContext)
{
ValidationResult validationResult = ValidationResult.Success;
var propertyTestedInfo = validationContext.ObjectType.GetProperty(OrderIDPropertyName);
if (propertyTestedInfo == null)
{
validationResult = new ValidationResult(string.Format("Unknown property {0}", OrderIDPropertyName));
}
else
{
int orderID = (int)propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
OrderStatus orderStatus = (OrderStatus)value;
if (orderID == 0 && orderStatus != OrderStatus.Future)
{
validationResult = new ValidationResult(string.Format("Order must have have an Order Status of Future when being created."));
}
}
return validationResult;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = ErrorMessage,
ValidationType = "ensureorderstatus"
};
rule.ValidationParameters["orderid"] = OrderIDPropertyName;
yield return rule;
}
}
「價值」的IsValid內的值總是OrderStatus的第一個枚舉值。我想知道是否需要採取其他步驟來確保DropDownList元素的選定值正確更新。
更新的代碼:
//OrderDetailsModel:
[DisplayName("Order Status"), EnsureOrderStatus("ID")]
public OrderStatus OrderStatus { get; set; }
public List<OrderStatusModel> OrderStatusModels = new List<OrderStatusModel>();
public OrderDetailsModel()
{
OrderStatusModels.Add(new OrderStatusModel { EnumStatus = OrderStatus.Active, StringStatus = "Active" });
OrderStatusModels.Add(new OrderStatusModel { EnumStatus = OrderStatus.Completed, StringStatus = "Completed" });
OrderStatusModels.Add(new OrderStatusModel { EnumStatus = OrderStatus.Future, StringStatus = "Future" });
}
<%= Html.DropDownListFor(model => model.OrderStatus, new SelectList(Model.OrderStatusModels, "StringStatus", "EnumStatus"))%>
我會試試這個......它看起來更像我在想什麼。一致認爲枚舉位是一個黑客。 –
再次。我試過了代碼,我真的很喜歡它的前進方向,但我不確定你最後一段代碼。我正在嘗試執行不顯眼的客戶端驗證。我已將我的EnsureOrderStatus屬性應用於OrderStatus屬性。雖然我看到將我的枚舉分解爲模型的許多其他好處 - 但我不明白(或者看到它影響)OrderStatus在IsValid內部始終默認爲其初始值這一事實。 –