如何使用占位符属性和Html.EditorFor?

时间:2022-09-26 20:59:04

I want to use the placeholder attribute in the Html.EditorFor so I did just like in the first answer: Html5 Placeholders with .NET MVC 3 Razor EditorFor extension?

我想在Html中使用占位符属性。第一个答案:Html5占位符。net MVC 3刀片编辑器扩展?

But in the final part, I don't have the EditorTemplates folder so I created it and when I tried to create the string.cshtml view I couldn't because the name "string" is reserved so I chose stringg.cshtml instead and it didn't work! Do I have to change the name elsewhere in my program? I did exactly like the answer...

但是在最后一部分中,我没有EditorTemplates文件夹,所以我创建了它,当我尝试创建字符串时。不能使用cshtml视图,因为名称“string”被保留,所以我选择了stringg。cshtml相反,它没有工作!我必须在程序的其他地方更改名称吗?我确实很喜欢这个答案……

Thank you!

谢谢你!

4 个解决方案

#1


44  

Upgrade to MVC 5.1 and you can use HTML attributes in EditorFor:

升级到MVC 5.1,你可以在EditorFor中使用HTML属性:

@Html.EditorFor(m => m.variable, new { htmlAttributes = new { placeholder = "Your Placeholder Text" } })

http://www.asp.net/mvc/overview/releases/mvc51-release-notes

http://www.asp.net/mvc/overview/releases/mvc51-release-notes

#2


7  

 @Html.EditorFor(model => model.members_ssn, new { htmlAttributes = new { @class = "form-control", placeholder = "Your Example Here" } })

#3


3  

When using TextBoxFor or TextAreaFor rather than EditorFor, use only the inner key-value pair from @Medo's answer, since the method directly expects an htmlAttributes object:

当使用TextBoxFor或TextAreaFor而不是EditorFor时,只能使用@Medo的答案中的键-值对,因为该方法直接期望htmlAttributes对象:

@Html.TextBoxFor(m => m.variable, new { placeholder = "Your Placeholder Text" })

(I realize this is tangential to the original question, but having this here would have helped me when I landed on this page earlier, looking for an answer to how to add placeholder text to an Html.TextAreaFor!)

(我意识到这与最初的问题是相切的,但是在这里有了这个问题,我就可以在前面的页面上找到如何向Html.TextAreaFor添加占位符文本的答案了!)

#4


0  

Use some jquery, eg: $("#FirstName").attr("placeholder", "Do not type name - use Search button");

使用一些jquery,例如:$(“#FirstName”)。attr(“占位符”,“不要键入名称-使用搜索按钮”);

#1


44  

Upgrade to MVC 5.1 and you can use HTML attributes in EditorFor:

升级到MVC 5.1,你可以在EditorFor中使用HTML属性:

@Html.EditorFor(m => m.variable, new { htmlAttributes = new { placeholder = "Your Placeholder Text" } })

http://www.asp.net/mvc/overview/releases/mvc51-release-notes

http://www.asp.net/mvc/overview/releases/mvc51-release-notes

#2


7  

 @Html.EditorFor(model => model.members_ssn, new { htmlAttributes = new { @class = "form-control", placeholder = "Your Example Here" } })

#3


3  

When using TextBoxFor or TextAreaFor rather than EditorFor, use only the inner key-value pair from @Medo's answer, since the method directly expects an htmlAttributes object:

当使用TextBoxFor或TextAreaFor而不是EditorFor时,只能使用@Medo的答案中的键-值对,因为该方法直接期望htmlAttributes对象:

@Html.TextBoxFor(m => m.variable, new { placeholder = "Your Placeholder Text" })

(I realize this is tangential to the original question, but having this here would have helped me when I landed on this page earlier, looking for an answer to how to add placeholder text to an Html.TextAreaFor!)

(我意识到这与最初的问题是相切的,但是在这里有了这个问题,我就可以在前面的页面上找到如何向Html.TextAreaFor添加占位符文本的答案了!)

#4


0  

Use some jquery, eg: $("#FirstName").attr("placeholder", "Do not type name - use Search button");

使用一些jquery,例如:$(“#FirstName”)。attr(“占位符”,“不要键入名称-使用搜索按钮”);