【问题标题】:Placeholder in @Html.EditorFor(model => model.Name) in MVC 5MVC 5 中 @Html.EditorFor(model => model.Name) 中的占位符
【发布时间】:2015-06-07 08:01:48
【问题描述】:

我在 Controller 中使用了[prompt("name")]

@Html.EditorFor(model => model.Email,
new {placeholder=ViewData.ModelMetadata.Watermark})`

在视图中但没有显示 我还使用了Html5 Placeholders with .NET MVC 3 Razor EditorFor extension? 的帮助 但什么也没发生 我需要 @EditorFor 的占位符而不是文本框 任何帮助表示赞赏

【问题讨论】:

  • 文本框有什么问题?您要生成什么类型​​的控件?对于 MVC-5,您可以使用 Html.EditorFor(m => m.Email, new { htmlAttributes = new { placeholder = ... }, })
  • @Html.EditorFor(model => model.Name, new { htmlAttributes = new { placeholder = "Enter the title Here" } }) 我已经写过了,但什么也没得到
  • 在 MVC-5 中?您已将其标记为 MVC-4 和 MVC-5!但为什么不TextBoxFor()
  • 查看为模型的所有属性生成的默认脚手架@EditorFor
  • 我假设您的意思是属性,而不是属性。但是为什么不把它改成TextBoxFor()(如果需要,添加type="email"

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


【解决方案1】:

您可以使用以下语法将 HTML 属性写入Html.EditorFor。确保在类和占位符之前使用 @,以便渲染器知道这是 HTML 标记。

@Html.EditorFor(model => model.Email, new { 
    htmlAttributes = new { 
        @class = "form-control", 
        @placeholder = "Email Address"
    }
})

【讨论】:

  • 通常只有像class 这样的保留关键字需要htmlAttributes 对象中的@ 前缀,“占位符”没有它也可以。但并不总是一致的;奇怪的是,设置“值”不会被解析为保留字,而是需要使用正确的大小写 (@Value) 才能正确应用。
【解决方案2】:

EditorFor 不接受 HtmlAttribut 参数,您必须使用 TextBoxFor 代替它;) 试试这个:

@Html.TextBoxFor(model => model.Email, new  {placeholder=ViewData.ModelMetadata.Watermark})`

【讨论】:

  • 谢谢 但是你知道为什么@EditoreFor 会这样吗? @TRIKI 哈立德
  • Html.EditorFor 适用于不同的类型,并会根据您作为参数输入的属性的类型生成具有不同类型的 Html 输入元素。您可以检查类并查看它们之间的检查差异:EditorExtensions 和 InputExtensions
  • 从 MVC 5.1 开始,EditorFor 可以处理 HtmlAttributes。这是添加的功能之一。
【解决方案3】:

只需使用 TextBoxFor 即可为我工作。

   @Html.TextBoxFor(model => model.emailContact, new {@placeholder = "Your Placeholder Text" }

【讨论】:

    猜你喜欢
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多