ASP.NET Dynamic Data: Displaying Custom Format And Default Value

By using DisplayFormatAttributes, you can specify how data fields are displayed and formatted by ASP.NET Dynamic Data. Here I have Flyer table I want to change FlyerDate format. I also have FlyerProduct table and I want to change the Price format. Note that Flyer has a relationship with FlyerProduct, hence FlyerProducts can be filtered by Flyer.

flyer-date

flyer-fk-date

 

First, you need to add a reference to System.ComponentModel.DataAnnotations into your project that contains the EDM.

reference to data annotations

Then, you specify the attributes of the partial class. Create a new class, which I called FlyerMetadata, in order to specify the
custom data formats. Overwriting ToString() will allow to display the custom format in the filters at the FlyerProducts page.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
 
[DisplayColumn("FlyerDate", "FlyerDate", true)]
[MetadataType(typeof(FlyerMetadata))]
public partial class Flyer
{
    public override string ToString()
    {
        return FlyerDate.ToString("MMM-dd-yyyy");
    }
}
 
public class FlyerMetadata
{
    [DefaultValue(true)]
    public bool IsValid { get; set; }
 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM-dd-yyyy}")]
    public DateTime FlyerDate { get; set; }
}

This is the result. Note that I also changed the format of the Price on the FlyerProduct partial class. Next post, I will show my solution to display images with a custom field template.

custom data format

custom data format filter

 

Advertisements

4 thoughts on “ASP.NET Dynamic Data: Displaying Custom Format And Default Value

  1. Hey there! Someone in my Myspace group shared this website with us
    so I came to take a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this
    to my followers! Fantastic blog and excellent design.

  2. You actually make it seem so easy with your presentation but I find this matter to be really something which
    I think I would never understand. It seems too complex and extremely broad for me.
    I’m looking forward for your next post, I’ll try to get the hang of it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s