我如何检查模型属性是否存在验证错误 - 我正在使用 validationmessagefor 方法来显示错误消息。我怎样才能一次只显示一条错误消息?我
这是我拥有的模型属性 m.CommunicationView.MobilePhone.Area m.CommunicationView.MobilePhone.Number
<div class="fl">
@Html.TextBoxFor(m => m.CommunicationView.MobilePhone.Area, new { @id = "personalDetailMobilePhoneAreaInput", @class="customText wdt80 numericValue",type = "text",maxlength="2" })
@Html.TextBoxFor(m => m.CommunicationView.MobilePhone.Number, new { @id = "personalDetailMobilePhoneNumberInput", @class="customText wdt120 mrglft10 phnIE numericValue",type = "text",maxlength="8" })
@Html.ValidationMessageFor(m => m.CommunicationView.MobilePhone.Area)
@Html.ValidationMessageFor(m => m.CommunicationView.MobilePhone.Number)
</div>
最佳答案
只需将 ErrorMessage
字段作为 [Required]
装饰器的一部分添加到您的模型中。例如数字:
[Required(ErrorMessage = "Number is required")]
[StringLength(10, ErrorMessage = "Number can be no larger than 10 characters")]
public string Number { get; set; }
关于c# - ValidationMessageFor 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27572953/