[显示(提示MVC3

发布于 2024-11-28 07:08:51 字数 943 浏览 1 评论 0原文

我正在尝试设置我的模型,以便我可以使用 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

折戟 2024-12-05 07:08:51

在您的控制器中,您需要执行以下

[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; }

操作 显示下的提示=“这将显示”是将要创建的水印。

然后您需要在 ~/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问题所以答案)。

In your controller you need to do the following

[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; }

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

@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue, new { @class="text-box single-line", placeholder = ViewData.ModelMetadata.Watermark })

More detailed information can be found at the link posted by tugberk (SO question and SO answer).

南…巷孤猫 2024-12-05 07:08:51

不幸的是,这不适用于 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.

神仙妹妹 2024-12-05 07:08:51

早期版本的 IE 不支持占位符属性,因此为了使 Display[(Promt="...")] 正常工作,我建议使用(以及 Samack 描述的 String 模板) )任何 jQuery 水印插件(我使用的是 google 的),然后将其添加到 Document.Ready 函数中:

$(document).ready(function(){
    $(':text').watermark({ textAttr: 'placeholder' });
})

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:

$(document).ready(function(){
    $(':text').watermark({ textAttr: 'placeholder' });
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文