ASP.NET MVC / Linq-to-SQL 类:我可以让它推断可读的显示名称吗?
如果我有一个带有字段 CustomerID
、OrderID
和 OrderDate
的表 Orders
,那么“Linq-to-SQL Classes”生成的类将称为 Orders
,其成员称为 CustomerID
、OrderID
和 OrderDate
。到目前为止,一切都很好。
但是,如果我随后执行 Html.LabelFor(m => m.OrderDate)
,那么生成的文本将是“OrderDate”而不是“Order Date”。
我尝试使用 Order_Date
作为字段名称,但这不起作用。有什么办法让它推断出更好的显示名称吗?
[我知道我可以使用数据注释来显式指定显示名称,但我真的不想为我的所有类/成员这样做 - 我只是希望它按约定工作。]
If I have a table Orders
with fields CustomerID
, OrderID
and OrderDate
, then the "Linq-to-SQL classes" generated class will be called Orders
, with members called CustomerID
, OrderID
and OrderDate
. So far so good.
However, if I then do Html.LabelFor(m => m.OrderDate)
then the generated text will be "OrderDate" instead of "Order Date".
I tried using Order_Date
as the field name, but that didn't work. Is there any way to get it to infer a better display name?
[I know that I can use data annotations to specify the display name explicitly, but I really don't want to do that for all my classes/members - I just want it to work by convention.]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您为此创建自己的 HTML Helper,例如
Html.MyLabelFor
。从此处应用的规则由您决定。您可以简单地按大小写拆分单词。
I suggest you create your own HTML Helper for this, something like
Html.MyLabelFor
.The rules to apply from here are up to you. You can simply split the word by case.
此问题的答案中包含可满足您要求的解决方案。 Asp.Net MVC 2 LabelFor 自定义文本。
There is a solution available for your requirements contained within the answer to this question. Asp.Net MVC 2 LabelFor Custom Text.
此方法利用现有的 MVC 2 架构在整个 *For 渲染方法上放置约定,而不是一次性 HTML 帮助程序,并且无需使用间隔属性名称重新标记所有内容。
如何“干燥”C# 属性本质
上,您所做的就是覆盖 MVC 2 的默认 ConventionModelMetadataProvider 行为,并为您提供挂钩以插入您自己的固定约定。
This method takes advantage of existing MVC 2 architecture to place conventions over the entire *For rendering methods instead of one off HTML helpers and without having to re-label everything with spaced property names.
How to "DRY up" C# attributes in Models and ViewModels?
Essentially what your doing is overriding the default ConventionModelMetadataProvider behavior of MVC 2 and providing hooks for you to insert your own opinionated conventions.