开发者

EditorTemplates in a class library

On an ASP.NET-MVC2 project, I have built some user controls that resides in Views\Sh开发者_开发技巧ared\EditorTemplates and DisplayTemplates folders.

I am trying to pull this templates into a class library, in order to make them reusable, how do I do that!

I don't know if this might help but a little more details; some of the templates are named after types like (string, Int32, etc) and some are named like RemoteCombo, and specified with a template hint in UIHint.


Unfortunately you can't just create a library and put ascx files there. You will have to create a 'code-only' version of your templates that renders appropriate html.

Microsoft had to do just that in order to deliver default templates inside System.Web.Mvc.dll

More information about templates can be found on this blog - it's a nice series of posts about templating in MVC. (take a look at 'Built-In Display Templates' section)

This is a default Editor Template for Decimals (from System.Web.Mvc.dll)

internal static string DecimalTemplate(HtmlHelper html)
{
    if (html.ViewContext.ViewData.TemplateInfo.FormattedModelValue == html.ViewContext.ViewData.ModelMetadata.Model)
    {
        html.ViewContext.ViewData.TemplateInfo.FormattedModelValue = string.Format(CultureInfo.CurrentCulture, "{0:0.00}", new object[] { html.ViewContext.ViewData.ModelMetadata.Model });
    }
    return StringTemplate(html);
}

All default templates can be seen in Reflector in System.Web.Mvc.Html namespace.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜