开发者

MVC 3 with Dynamic Data - applying data types to Dynamic Data

I have an MVC 3 C# / ADO.NET / Dynamic Data app set u开发者_Go百科p and working(ish). To set it up I created an MVC 3 app, added the Dynamic Data components in, split out Presentation, Business and Data in to three projects, set the references to match the MVC pattern and set up the routes and scaffolding.

List, Edit and Insert all work with the standard DD page templates, however I've hit a wall getting the Presentation Layer to apply data type attributes to the data displayed in Gridview and Details views, particularly for URLs, which I want be typed as DataType.Url and so use the associated DD display attributes.

Have tried setting up a meta data class for the Link table and applying something like:

[DataType(DataType.Url)]
public object URL {get; set;}

(the Url field in table "Link" is "URL")

.. within a partial class, which is something I read about for pure DD sites.

Can anyone point me in the right direction, or tell me if is this even possible?

Many Thanks.


Yes this is possible. I would write a custom FieldTemplate for Urls. Using the UIHint metadata, you can assign a custom fieldtemplate to the column. Something like this (untested):

    public partial class FooUrl : System.Web.DynamicData.FieldTemplateUserControl
    {

        string getUrl()
        {
            var metadata = MetadataAttributes.OfType<DataTypeAttribute>().FirstOrDefault();
            if (metadata == null)
                return FieldValueString;
            switch (metadata.DataType)
            {
                case DataType.Url:
                    string url = FieldValueString;
                    if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
                        url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                        return url;

                    return "http://" + FieldValueString;
                case DataType.EmailAddress:
                    return "mailto:" + FieldValueString;
                default:
                    throw new Exception("Unknown DataType");
            }
        }



        protected override void OnDataBinding(EventArgs e)
        {
            HyperLinkUrl.NavigateUrl = getUrl();
        }

        public override Control DataControl
        {
            get
            {
                return HyperLinkUrl;
            }
        }
    }
}

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜