ASP.NET MVC 3 - 文本框渲染的数据注释和最大长度/大小
我知道在 Razor View 文件中,我们可以做这样的事情 @Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" })
但是,我希望为 MVC 创建一个模型,可用于创建一个具有显式定义的大小和文本框的最大长度。我在模型的属性之上尝试 [StringLength(n)] ,但这似乎只是进行验证而不是设置文本框的大小。
无论如何,我们是否可以将文本字段的长度定义为模型属性之上的数据注释?
因此,最终,我们可以通过使用 razor 映射到模型来创建整个表单,而不是显式地一一选取模型属性来设置文本框大小。
I know on the Razor View file, we can do something like this
@Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" })
However, I am hoping to create a model for the MVC that can be used to create a form with explicitly defined the size and max length of the textboxes. I try [StringLength(n)] on top of the properties of the model, but that seems to only do the validation ratherh set the size of the textbox.
Is there anyway that we can define the length of the text field as a data annotation on top of a property of a model?
So ultimately, we could just create the whole form by using razor to map to a model rather than explicitly pick up the model properties one by one in order to set the textbox size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是使用
StringLengthAttribute
的自定义帮助程序的概述。Here is a outline of a custom helper that uses
StringLengthAttribute
.这不行吗?
在视图中:
或:
Does this not work?
In the View:
or:
我发现我更喜欢只调用 Html.EditorFor(...) 的观点。这意味着编辑器和显示模板决定了我视图中控件的命运,这样我的视图代码就被清理了很多 - 它只有 html 和编辑器的通用请求。
以下链接提供了在编辑器模板中实现此功能的工作示例
https://jefferytay.wordpress.com/2011/12/20/asp-net-mvc-string-editor-template-which-handles-the-stringlength-attribute/
我'我在我的 String.cshtml 编辑器模板中使用类似的内容(位于 Shared/EditorTemplates 中)。
然后我的 model 被注释,例如:
我的 View 只是调用:
您可能还注意到我的 String.cshtml 编辑器模板也自动神奇地处理 Enum,但这正在开始偏离当前主题,所以我取消了该代码,我只是在这里说字符串编辑器模板可以增加额外的重量,并且谷歌可能对此有一些https://www.google.com/search?q=string+editor+template +enum
Label With Tooltip For 是一个自定义 HTML 帮助器,只需将说明放入标签标题中,即可获取有关将鼠标悬停在每个标签上的更多信息。
如果您想在编辑器模板中执行此操作,我建议您使用此方法。
I find that I prefer my views to just Call Html.EditorFor(...). This means that the Editor and Display templates decide the fate of controls in my view, such that my view code gets cleaned up a lot - it just has html and generic requests for editors.
The following link gives a working sample of getting this working in an Editor Template
https://jefferytay.wordpress.com/2011/12/20/asp-net-mvc-string-editor-template-which-handles-the-stringlength-attribute/
I'm using similar in my String.cshtml Editor Template (goes in Shared/EditorTemplates ).
Then my model is annotated such as:
And my View simply calls:
You might also notice that my String.cshtml Editor Template also auto-magically handles Enum's, but that is starting to digress from the current topic, so I nixed that code, I'll just say here that the String Editor Template can pull extra weight, and likely google has someting on that https://www.google.com/search?q=string+editor+template+enum
Label With Tooltip For is a custom HTML helper that just drops the description into the label title, for more information on mouse over for every label.
I'd recommend this approach if you want to do this in an Editor Template.