c# - Show localized display name for MetaTable and MetaColumn in ASP.NET Dynamic Data website -
i'm using visual studio 2017 asp.net dynamic data project template create website database crud operations models.
i'm setting displayname
in database model's metatable
, , can shown in asp.net dynamic data website, this:
[scaffoldtable(true)] [metadatatype(typeof(itemmetadata))] public class itemmodel { public guid id { get; set; } ... ... } [displayname("thisismyitemmodeldisplayname")] class itemmetadata { [scaffoldcolumn(true)] [display(order = 1)] public guid id; [scaffoldcolumn(true)] [display(name = "thisisitemname", order = 3)] public string itemname; ... }
it's good, website can reflect , show string i've put in, website need support different locale languages, created customized attribute allow pass in resource string key,
[attributeusage(attributetargets.class | attributetargets.field, allowmultiple = false)] public class localizedisplayname : attribute { public localizedisplayname(string localizedstringkey) { this.stringkey = localizedstringkey; } public string stringkey { get; } }
use this:
[localizedisplayname("dynamicdatawebsite_model_item_localizeddisplayname")] class itemmetadata { [scaffoldcolumn(true)] [display(order = 1)] public guid id; [scaffoldcolumn(true)] [localizedisplayname(name = "dynamicdatawebsite_model_item_column_itemname", order = 3)] public string itemname; ... }
and in website pages code showing model, can resolve localized string key resource file, localized string:
var target = metatable.attributes[typeof(localizedisplayname)]; var localizedstr = resources.strings.resourcemanager.getstring(((localizedisplayname)target).stringkey, cultureinfo.currentculture);
above code easy inject code in aspx.cs, gridview , etc., of binding logical behind scence, sample code visual studio dynamic data website template's list page
showing list of values model:
<asp:gridview id="gridview1" runat="server" datasourceid="griddatasource" enablepersistedselection="true" allowpaging="true" allowsorting="true" cssclass="ddgridview" rowstyle-cssclass="td" headerstyle-cssclass="th" cellpadding="6"> <columns> <asp:templatefield> <itemtemplate> <asp:linkbutton runat="server" commandname="edit" text="edit" onclientclick='return confirm("super admin, sure want edit item?");' /> <asp:linkbutton runat="server" commandname="delete" text="delete" onclientclick='return confirm("super admin, sure want delete item?");' /> <asp:dynamichyperlink runat="server" text="details" /> </itemtemplate> </asp:templatefield> </columns> <pagerstyle cssclass="ddfooter" /> <pagertemplate> <asp:gridviewpager runat="server" /> </pagertemplate> <emptydatatemplate> there no items in table. </emptydatatemplate> </asp:gridview>
for here, want header row showing localized string marked in metatable's metacolumn, column itemname
marked resource key
dynamicdatawebsite_model_item_column_itemname
so how can achieve generic way suits types of models?
wiki
Comments
Post a Comment