Rozszerzanie MVC RequiredAttribute
Mam rozszerzoną klasę RequiredAttribute, która nie wysyła z powrotem komunikatów o błędach. Jeśli sprawdzę to w debuggerze, tekst jest w porządku.
public class VierRequired : RequiredAttribute
{
public VierRequired(string controlName)
{
//...
}
public string VierErrorMessage
{
get { return ErrorMessage; }
set { ErrorMessage = value; }
}
// validate true if there is any data at all in the object
public override bool IsValid(object value)
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
return true;
return false; // base.IsValid(value);
}
}
Nazywam to w ten sposób
[VierRequired("FirstName", VierErrorMessage = "Please enter your first name")]
public string FirstName { get; set; }
I widok mvc
<%: Html.TextBoxFor(model => model.FirstName, new { @class = "formField textBox" })%>
<%: Html.ValidationMessageFor(model => model.FirstName)%>
Działa, gdy używam normalnej wymaganej adnotacji
[Required(ErrorMessage = "Please enter your name")]
public string FirstName { get; set; }
Ale zwyczaj nie wysyła żadnych komunikatów o błędach