In the original: MVC dynamic property setting validation for a model class
There is a self-contained validation mechanism in MVC, such as if the type of a field is a number or a date, then the compiler will automatically validate and prompt the user for an incorrect format when the user enters Chinese characters or English characters, but after all this verification is limited, we need to customize the validation ourselves.
Suppose there is a model class: Class dinners{
private string Title;
Private System.DateTime eventdate;
private string Description;
private string Hostedby;
private string Contactphone;
private string Address;
private string country;
Private double Latitude;
private double longitude;
}
You need to add a non-nullable constraint to each property of the class, and verify the phone number and date. The code is as follows:
public partial class dinners
{
public bool IsValid
{
get {return (Getruleviolations (). Count () = = 0); }
}
Public ienumerable<ruleviolation> getruleviolations ()
{
if (String.IsNullOrEmpty (Title))
{
Yield return new Ruleviolation ("title required", "title");
}
if (String.IsNullOrEmpty (Description))
{
Yield return new Ruleviolation ("Description required", "Description");
}
if (String.IsNullOrEmpty (Hostedby))
{
Yield return new Ruleviolation ("Hostedby required", "Hostedby");
}
if (String.IsNullOrEmpty (Address))
{
Yield return new ruleviolation ("Address required", "address");
}
if (String.IsNullOrEmpty (country))
{
Yield return new ruleviolation ("Country required", "country");
}
if (String.IsNullOrEmpty (Contactphone))
{
Yield return new Ruleviolation ("phone# required", "Contactphone");
}
//Refer to class Phonevalidator (custom validation class, no longer detailed)
if (! Phonevalidator.isvalidnumber (Contactphone, country))
{
Yield return new Ruleviolation ("phone# does not match country", "Contactphone");
}
yield break;
}
partial void OnValidate (Changeaction action)
{
if (! IsValid)
{
throw new ApplicationException ("Rule violations Prevent saving");
}
}
}
public class Ruleviolation
{
public string ErrorMessage {get; private set;}
public string PropertyName {get; private set;}
Public Ruleviolation (String errormessage, String propertyname)
{
ErrorMessage = errormessage;
propertyname = PropertyName;
}
}
Foreground Verification Code:
<%=html.validationsummary ("Please crrect the errors and try again.")%>
<% using (Html.BeginForm ()) {%>
<fieldset>
<p>
<label for= "Title" >dinner title:</label>
<%=html.textbox ("Title")%>
<%=html.validationmessage ("Title", "*")%>
</p>
<p>
<label for= "Eventdate" >EventDate:</label>
<%=html.textbox ("Eventdate", String. Format ("{0:g}", model.eventdate))%>
<%=html.validationmessage ("Eventdate", "*")%>
</p>
<p>
<label for= "Description" >Description:</label>
<%=html.textarea ("Description")%>
<%=html.validationmessage ("Description", "*")%>
</p>
<p>
<label for= "Address" >Address:</label>
<%=html.textarea ("Address")%>
<%=html.validationmessage ("Address", "*")%>
</p>
<p>
<label for= "Country" >Country:</label>
<%=html.textbox ("Country")%>
<%=html.validationmessage ("Country", "*")%>
</p>
<p>
<label for= "Contactphone" >contactphone #:</label>
<%=html.textbox ("Contactphone")%>
<%=html.validationmessage ("Contactphone", "*")%>
</p>
<p>
<label for= "Latitude" >Latitude:</label>
<%=html.textbox ("Latitude")%>
<%=html.validationmessage ("Latitude", "*")%>
</p>
<p>
<label for= "Longitude" >Longitude:</label>
<%=html.textbox ("longitude")%>
<%=html.validationmessage ("Longitude", "*")%>
</p>
<p>
<input type= "Submit" value= "Save"/>
</p>
</fieldset>
<%}%>
<div>
<%= Html.ActionLink ("Back to List", "Index")%>
</div>
Note: The code is from the tutorial "step-by-step learning of ASP. NET MVC"