我正在尝试设置我的模型,这样我就可以使用@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")]
但由于某种原因,它没有出现在我的视图中。
顺便说一句。我确实试着按照这篇文章的指示去做,Html5 Placeholders with .NET MVC 3 Razor EditorFor extension?
在这篇文章的末尾,它说要更改~/Views/Shared/EditorTemplates/String.cshtml文件,但这个文件不在我的项目中。
如有任何提示,我们将不胜感激。提前谢谢你。
跟进!
啊,MVC3的乐趣。显然,一旦你理解了发生了什么,上面的帖子就回答了这个问题。如果你在你的~/Views/Shared/文件夹中创建了EditorTemplates/String.cshtml文件,那么它将使用这个模板来创建你的editfor框。
为了让其他人看起来更简洁,最后的答案将在下面发布。
发布于 2011-08-09 22:18:07
在您的控制器中,您需要执行以下操作
[Display(Prompt="First Name Goes Here",Name="First Name")]
[StringLength(100,ErrorMessage="First Name may not be longer than 100 characters")]
public string AuthFirstName { get; set; }display下的Prompt="This is what will display“是将要创建的水印。
然后,您需要在~/Views/Shared下创建文件夹"EditorTemplates“,整个路径为~/Views/Shared/EditorTemplates/
然后创建文件String.cshtml并将以下代码放入其中
@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue, new { @class="text-box single-line", placeholder = ViewData.ModelMetadata.Watermark })更详细的信息可以在tugberk发布的链接(SO question和SO answer)中找到。
发布于 2012-07-14 04:44:49
不幸的是,这不适用于IE。至少是IE9和更早的版本。我花了几个小时的时间来解释为什么这对别人有帮助,而在这里却不起作用。显然IE不会显示提示。希望IE10能解决这个问题。
发布于 2014-08-18 08:18:43
早期版本的IE不支持占位符属性,所以为了让Display[(Promt="...")]正常工作,我建议使用(连同Samack描述的字符串模板一起)任何jQuery水印插件(我使用的是google one),然后将以下代码添加到Document.Ready函数中:
$(document).ready(function(){
$(':text').watermark({ textAttr: 'placeholder' });
})https://stackoverflow.com/questions/6960673
复制相似问题