【问题标题】:MVC5 placeholder for input not working输入的 MVC5 占位符不起作用
【发布时间】:2015-01-12 12:10:49
【问题描述】:

我的代码是这样的

 @Html.EditorFor(model => model.CreatedUser, new { htmlAttributes = new { @class = "form-control" ,@placeholder = "Your Placeholder Text"  }})

我希望占位符在我在代码中添加时出现,但它没有显示。谁能指出我做错了什么? 输出 HTML

<input class="text-box single-line" id="CreatedUser" name="CreatedUser" type="text" value="" autocomplete="off" style="cursor: auto;  background-attachment: scroll; background-position: 100% 50%; background-repeat: no-repeat;">

【问题讨论】:

  • class 有效吗?发布输出的 HTML
  • 这不适用于EditorFor
  • @mattytommo Nop。上课也不行。添加了输出 HTML
  • htmlAttributes 分配给EditorFor() 仅适用于MVC-5.1
  • 只有当您为类型创建自定义EditorTemplate 时,您才能将属性作为ViewDataDictionary 传递。我建议你使用TextBoxFor()

标签: c# asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

接受htmlAttributesEditorFor 重载仅适用于MVC 5.1。

HERE

如果无法升级,请使用TextBoxFor

@Html.TextBoxFor(model => model.CreatedUser, new { @class = "form-control" ,@placeholder = "Your Placeholder Text" })

【讨论】:

  • 有没有办法在 5.0 中将占位符和类一起添加到“EditorFor”??
  • @Athul 不幸的是没有。
  • @mattytommo 为什么不使用 TexBoxFor?
  • @EhsanSajjad 这就是我在回答中所说的?在某些情况下(例如自定义编辑器模板)使用TextBoxFor 可能不合适。
猜你喜欢
  • 1970-01-01
  • 2016-10-17
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2018-02-01
  • 2016-04-24
  • 2023-04-08
相关资源
最近更新 更多