我正在寻找这里给出的答案: HTML/CSS Making a textbox with text that is grayed out, and disappears when I click to enter info, how?
但我想在MVC4中这样做。
我得到了以下观点:
@using (Html.BeginForm("Kompetens", "KumaAdmin"))
{
<div class="three columns" style="margin-right: 627px;">
<h6>Kompetens</h6>
<div style="width:456px;"> @Html.ListBox("kompetensId", (SelectList)ViewBag.KomId)</div><br/>
<h6>Lägg till kompetens</h6>
<div class="focus">
@Html.EditorFor(mm => mm.KompetensTest)
</div>
<input type="submit" style="margin-right: 205px;" value="Skapa"/><br/><br/>
</div>
}
因为这是我的文本框:
@Html.EditorFor(mm => mm.KompetensTest)
我不知道如何应用“onfocus”&amp; onblur属性就像上面的链接一样。
我对MVC很陌生,所以如果这是一个新手问题,请与我联系。
谢谢!
答案 0 :(得分:1)
您需要创建一个编辑器模板。因为Html.EditorFor没有“object htmlattributes”参数来执行“new {onfocus =”js here“}”。
通过视图&gt;共享,
创建一个名为EditorTemplates的文件夹
然后,使用@model string / whathever这个对象创建一个视图。根据需要命名文件。
当您将@model放在视图上时,您指定它只接受此类型mas模型。
在此视图中,您创建了一个Html.TextBox(而不是TextBoxFor)并且瞧瞧。
在Html.EditorFor方法上,还有一种方法可以设置要使用的编辑器模板。通过键入以下名称来选择您创建的名称:
@Html.EditorFor(mm => mm.KompetensTest, "GreyedTemplate")
视图代码我命名为:GreyedTemplate.cshtml
@model string
@Html.TextBox("", Model, new { onfocus = "", onclick="" })
请注意,第一个参数为空。这是故意的,因为当你使用EditorFor(mm =&gt; mm.KompetensTest,“GreyedTemplate”)时,它会自动使用KompetensTest作为字段的名称。
答案 1 :(得分:0)
您想使用占位符html属性(http://www.w3schools.com/tags/att_input_placeholder.asp)
像@Html.EditorFor(mm => mm.KompetensTest, new { placeholder = "Text" })
答案 2 :(得分:0)
@Gmoliv最终工作!我googeld arround,发现“Editfor”无法访问html属性。虽然我发现可以访问它们的“TextBoxFor”,所以解决方法是:
@Html.TextBoxFor(mm => mm.Profile, new { placeholder = "Ange Profil" })
@Pedro我真的很努力使它工作,但问题是我无法设置值,所以它是alwasy空,我treid设置它在视图和templateView中它只是没有采取。如果你能,我会很感激完整的代码示例
非常感谢!