The use of Metadatatype, MVC's model layer data validation

Source: Internet
Author: User

Activityflowcontent is the model class of the solid models

Activityflowcontentmetadata is the model class that he wrote.

The same two class attributes can form a mapping relationship, and additional fields can be added to the activityflowcontent

[Metadatatype (typeof (Activityflowcontentmetadata))]

public partial class Activityflowcontent:ibaseentity
{

[Display (Name = "Activity Flow Classification")]
public string Activityclassificidold {get; set;}

#region Custom Properties

#endregion

}
public class Activityflowcontentmetadata
{
[Scaffoldcolumn (False)]
[Display (Name = "PRIMARY Key", Order = 1)]
public string ID {get; set;}

[Scaffoldcolumn (True)]
[Stringlength (errormessage = "Length not exceeding 36")]
[Display (Name = "Activity class ID", Order = 2)]
Public object Activityclassificid {get; set;}

[Scaffoldcolumn (True)]
[Stringlength (errormessage = "Length not exceeding 200")]
[Display (Name = "Activity Summary", Order = 3)]
Public object Sumary {get; set;}

[Scaffoldcolumn (True)]
[DataType (datatype.multilinetext,errormessage= "character is not formatted correctly")]
[Display (Name = "active Content", Order = 4)]
Public object Content {get; set;}

[Scaffoldcolumn (True)]
[Stringlength (errormessage = "Length not exceeding 50")]
[Display (Name = "keyword", Order = 5)]
Public object Keywords {get; set;}


}

The use of Metadatatype, MVC's model layer data validation
Specifies the metadata class to associate with the data model class

Using System.ComponentModel.DataAnnotations;

Specifies the metadata class to associate with the data model class
[Metadatatype (typeof (Product_metadata))]
public partial class Product
{
Public Product ()
{
This. Productguid = Guid.NewGuid ();
This. Addtime = DateTime.Now;
}
public class Product_metadata
{
Public Guid Productguid {get; set;}
[Required (errormessage = "Product name cannot be empty")]
public string ProductName {get; set;}
Public DateTime addtime {get; set;}
}
}

With this association, you can do some initialization work, and you can do some validation, as well as custom validation error messages, which are equivalent to extending the model entity
Because validation is done in the model layer, so good maintenance, so need to do metadata, and the ADO Entity Data Model associated.
For example, the product price needs to do an input verification, how to do it, please see
[RegularExpression (@ "^ (\d+) (. \d+)?", errormessage = "Price format error")]
[Required (errormessage = "*")]
Public double Productprice {get; set;}
Right, is not very good to expand, later need to modify the validation, as long as the metadata class inside the change can be.

When the controller layer is called, the validation code
if (! Modelstate.isvalid)
{
return View ();

}

The use of Metadatatype, MVC's model layer data validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.