目标
成功应用@Html.Textbox方法的placeholder属性。
问题所在
在我的应用程序上有以下语法;
@Html.TextBox("term", new { placeholder = "What are you searching for?" })但是,当呈现TextBox时,input的value属性为placeholder = "What are you searching for?"。换句话说,placeholder属性不是作为属性应用的,而是作为input的value应用的。
知识
我已经在Google和Stack Overflow上搜索过这个问题,但到目前为止,还没有成功。
This link有一个与我使用的语法相同的解决方案,但是当我将第二个参数传递给TextBox()时,它被呈现为一个值,而第三个参数(在我们的例子中是new { placeholder = "something" })不会发生任何变化。
发布于 2013-08-14 21:25:27
您调用的是that method的string name, object value overload,因此第二个参数将被用作value,而不是htmlAttributes。您应该使用该方法的不同重载,可能是通过指定一个空value来实现string name, object value, object htmlAttributes
@Html.TextBox("term", "", new { placeholder = "What are you searching for?" })发布于 2018-01-28 19:06:15
对空的@Html.TextBox尝试此操作
@Html.TextBox("CustomarName" ,null, new { @class = "form-control" , @placeholder = "Search With Customar Name" })发布于 2013-08-14 21:23:42
您还需要第三个参数:
@Html.TextBox("term", Model.SomeProperty, new { placeholder = "What are you searching for?" })
第三个参数是您希望包含在输入字段的HTML输出中的任何属性。
https://stackoverflow.com/questions/18232902
复制相似问题