如何在MVC中向业务对象添加帮助提示属性并在视图中显示
如何在 ASP.Net MVC 应用程序中针对我的业务对象提供某种形式的属性,以便我可以在视图中选取帮助文本并将它们显示为弹出悬停帮助文本,如下所示:
<%= Html.TextBox("PostalCode", Model.PostalCode, new {
title = "Please enter your postal code." })%>
我希望能够从我的 ViewModel 获取“标题”值,该视图模型将从业务对象的属性中获取文本。
总之,如何在我的业务对象上应用帮助文本属性/元数据?
How can I provide some form of Attribute against my Business Objects in my ASP.Net MVC application so that I can pick up the help texts in the View and have them appear as pop-up hover help texts like this:
<%= Html.TextBox("PostalCode", Model.PostalCode, new {
title = "Please enter your postal code." })%>
I want to be able to get the 'title' value from my ViewModel which will have picked up the text from an attribute on the Business Object.
In summary, how can I apply help text attributes / metadata on my Business Objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我是这样做的:
创建新属性如下:
向实体属性添加了属性,如下所示:
向实体添加扩展方法,如下:
添加了一个 HtmlHelper(简单),如下:
最后在视图中使用了以下标记:
尽管需要改进,但它完成了工作。 ;)
Here is how I did it:
Created new Attribute as follows:
Added attribute to entity property as follows:
Added Extension Method to Entities as follows:
Added a HtmlHelper (simple) as follows:
And finally used the folowing markup in the View:
This does the job although it needs refinement. ;)
我认为没有一种方法可以直接开箱即用。
不过,您可以做的是创建一个通用的“模型值”类,将这些信息封装在其中,从而保留您的强类型视图。即:
然后您可以构建模型类来包含 ModelValue 类型的属性。
上面的代码看起来像这样:
这样做的缺点是我不认为 mvc 会为你做自动绑定,所以你必须自己在视图中完成所有映射,就像在这个例子中一样而且在 Post 上,如果您还没有手动装订,则必须进行手动装订。
您可能还会在模型的构造函数中实例化所有 ModelValue 属性,并从存储的位置提取所有标题值,因为您不会在 Post 上重新绑定它们(我正在考虑这里的验证错误,导致表单被重新显示)。
如果您非常热衷,您可以在模型属性上添加属性,然后在渲染页面时以某种方式解析它们,但如果您想这样做,我不知道从哪里开始。
I don't think there is a way to do it straight out of the box.
What you could do though is create a generic "model value" class that encapsulated that information in it thus keeping your strongly typed view. ie:
You could then build up your model classes to contain properties of type ModelValue.
Your code above would then look something like:
The down side to doing this is that I don't think mvc is going to do automatic binding for you, so you'll have to do all the mapping in the view yourself like in this example but also on Post you will have to do manual binding if you aren't already.
You would probably also instantiate all your ModelValue properties in the constructor of the model and pull in all the title values from whever they are stored then because you wouldn't rebind them on Post (I am thinking about validation errors here causing the form to be redisplayed).
If you were super keen you would put attributes on your model properties and then somehow have them parsed when you render the page, but I have no idea where you would start if you wanted to go this way.
更新:
您可以为每个名为“Title”或“Hint”的对象创建一个新的类属性,并向它们添加适当的字符串值。然后使用 MyObject.Title 获取该属性
有趣的问题。我希望看到使用属性对此问题的答案,但我可以想到两种方法:
向对象添加扩展方法
这将需要大量重复代码。
Html Helper 扩展方法
您可以在此 helper 方法中存储对象标题。
调用此方法:
或者在您的示例中:
我更喜欢第二种方法,因为您有一个地方可以存储所有标题,并且需要编写更少的代码。
但是,我认为向类添加一个属性并创建一种获取该属性的方法会更好一些。
Update:
You could just create a new class property for each of your object named "Title" or "Hint" and add the appropriate string value to them. Then get that property with MyObject.Title
Interesting question. I would like to see an answer to this using attributes, but here are two methods I can think of:
Add an extension method to your objects
This would require alot of repetitive code.
Html Helper extension method
You would store the object titles in this helper method.
To call this method:
Or in your example:
I prefer the 2nd method because you have a place to store all the titles and you would need to write less code.
However, I think adding an attribute to the class and creating a means to get that attribute would work a bit better.
我知道这已经很旧了,但我最近在 ASP.NET MVC3 项目中遇到了这个问题并实现了一个解决方案。
1.创建自定义属性来存储帮助文本
2.创建一个 HtmlHelper 扩展方法来检索属性值
3.使用 HelpText 属性注释模型属性
4.只需在您的视图中使用新的 HtmlHelper 扩展方法
I know this is old, but I faced this problem recently with an ASP.NET MVC3 project and implemented a solution.
1. Create a custom attribute to store the help text
2. Create a HtmlHelper extension method to retrieve the attribute value
3. Annotate the model property with the HelpText attribute
4. Simply use the new HtmlHelper extension method with your view