[显示(提示MVC3
我正在尝试设置我的模型,以便我可以使用 @Html.EditorFor(e => e.publicationTitle)
并让它显示带有提示的水印。
目前我正在做,
@Html.LabelFor(e => e.PublicationTitle) @Html.TextBox("PublicationTitle",tempTitle.ToString(), new { style = "width:350px", placeholder = "Put title here" })
@Html.ValidationMessageFor(e => e.PublicationTitle)
我发现您可以在我的模型中放置 [Display(Prompt="Enter title here")]
,
但由于某种原因它没有显示在我的视图中。
附带说明一下。我确实尝试按照这篇文章中的说明进行操作 带有 .NET MVC 3 Razor Editor 的 Html5 占位符用于扩展?
在这篇文章的末尾,它说要更改 ~/Views/Shared/EditorTemplates/String.cshtml 文件,但该文件不在我的项目中。
任何提示将不胜感激。先感谢您。
跟进!
MVC3 的乐趣啊。显然,一旦您了解发生了什么,上面的帖子就可以回答问题。如果您在 ~/Views/Shared/ 文件夹中创建 EditorTemplates/String.cshtml 文件,那么它将将此模板用于您的 editfor 框。
最终答案将在下面发布,以供其他人查看。
I am trying to setup my model so I can use the@Html.EditorFor(e => e.publicationTitle)
and have it show a Watermark with a hint.
Currently I am doing
@Html.LabelFor(e => e.PublicationTitle) @Html.TextBox("PublicationTitle",tempTitle.ToString(), new { style = "width:350px", placeholder = "Put title here" })
@Html.ValidationMessageFor(e => e.PublicationTitle)
I have found that you can put a [Display(Prompt="Enter title here")]
in my model
but it is not showing up in my view for some reason.
On a side note. I did try to follow the instructions from this post
Html5 Placeholders with .NET MVC 3 Razor EditorFor extension?
At the end of this post it says to change the ~/Views/Shared/EditorTemplates/String.cshtml file but this file is not located in my project.
Any hints would be appreciated. Thank you in advance.
FOLLOW UP!
Ah the joys of MVC3. Apparently the above post answers the question once you understand what is going on. If you create the EditorTemplates/String.cshtml file in your ~/Views/Shared/ folder then it will use this template for your editfor boxes.
Final Answer to be concise for others looking will be posted below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的控制器中,您需要执行以下
操作 显示下的提示=“这将显示”是将要创建的水印。
然后您需要在 ~/Views/Shared 下创建文件夹“EditorTemplates”,整个路径将为 ~/Views/Shared/EditorTemplates/
然后创建文件 String.cshtml 并将以下代码放入其中
更详细的信息可以找到在 tugberk 发布的链接(SO问题和所以答案)。
In your controller you need to do the following
The Prompt="This is what will display" under display is the watermark that will be created.
Then you will need to create the folder "EditorTemplates" under ~/Views/Shared the entire path will be ~/Views/Shared/EditorTemplates/
Then create the file String.cshtml and place the following code in it
More detailed information can be found at the link posted by tugberk (SO question and SO answer).
不幸的是,这不适用于 IE。至少 IE9 及更早版本。我花了几个小时揪着自己的头发,想知道为什么这对别人有帮助,但在这里不起作用。显然 IE 不会显示提示。希望IE10能够解决这个问题。
This will not work with IE unfortunately. At least IE9 and earlier. I spent couple hours pulling my hair as to why this helped others and doesn't work here. Apparently IE won't show prompts. Hope IE10 will address the issue.
早期版本的 IE 不支持占位符属性,因此为了使
Display[(Promt="...")]
正常工作,我建议使用(以及 Samack 描述的 String 模板) )任何 jQuery 水印插件(我使用的是 google 的),然后将其添加到 Document.Ready 函数中:The placeholder attribute is not supported in earlier versions of IE, so for the
Display[(Promt="...")]
to work fine I recommend to use (along with the String template as Samack described) any jQuery watermark plugin (I used the google one) then add this to the Document.Ready function: