且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ModelMetadata.Watermark和MVC视图模型

更新时间:2023-11-29 21:01:52

你会得到一个空水印的原因是,在你的情况下(即不使用模板)的的ViewData ​​ em>的实际上是指 myFormModel (不是 myFormModel.firstFieldValue );你基本上是检索您的视图模型的水印。由于模型不能有水印( [显示] 不能应用于类) ViewData.ModelMetadata.Watermark 永远是空的观点。

The reason you get an empty watermark is that in your case (i.e. not using templates) ViewData actually refers to myFormModel (not myFormModel.firstFieldValue); you are essentially retrieving the watermark of your view model. Since models can't have watermarks ([Display] can't be applied to classes) ViewData.ModelMetadata.Watermark will always be empty for views.

据我所看到的,在这里你唯一的选择(如果你不想使用模板)做水印嵌入:

As far as I can see, your only option here (if you don't want to use templates) is doing the watermark inline:

@Html.TextBoxFor(x => x.firstFieldValue, new { @class = "myInputStyle", placeholder = "Your watermark text here" })

顺便说一句,如果要使用模板,您需要使用模板助手 @ Html.EditorFor() @ Html.DisplayFor () @ Html.TextBoxFor()仅仅是强类型 @ Html.TextBox()版本。它不是模板

By the way, if want to use templates, you need to use the templated helpers @Html.EditorFor() and @Html.DisplayFor(). @Html.TextBoxFor() is just the strongly-typed version of @Html.TextBox(). It is not templated.