ModelMetadata.Watermark和MVC视图模型 [英] ModelMetadata.Watermark and MVC View Models

查看:152
本文介绍了ModelMetadata.Watermark和MVC视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎在我MVC3表单输入显示的水印(所谓的占位符)。

I'm struggling to display a watermark (so called placeholder) on my MVC3 form inputs.

有已经几个帖子这儿说话,包括非常注重一个在这里:

There is already few posts down here talking including the quite focused one here:

<一个href=\"http://stackoverflow.com/questions/5824124/html5-placeholders-with-net-mvc-3-razor-editorfor-extension/\">Html5使用.NET MVC 3剃须刀EditorFor扩展占位符?

在这些帖子,建议由创建扭捏html.TextBox模板。
就我而言,我的资产,我应该不需要任何编辑器模板作为我行内调整他们。

In these posts, advice is made to create tweaked html.TextBox templates. In my case, my asset is that I should not need any editor template as I'm tweaking them inline.

,这里是实际code的相关部分:

Better than long talks, here is the relevant part of the actual code:

〜/模型/ myModel.cs

~/Models/myModel.cs

namespace myProject.Models {
  public class myFormModel {
    ...
    [Display(Name = "firstFieldName", Prompt = "firstFieldPrompt")]
    public string firstFieldValue { get; set; }
    ...
  }
}

〜/控制器/ myFormSurfaceController.cs

~/Controllers/myFormSurfaceController.cs

namespace myProject.Controllers {
  public class myFormSurfaceController : SurfaceController {
    ...
    [ChildActionOnly]
    public PartialViewResult myForm()
    {
        return PartialView("myPartialView", new myFormModel());
    }
    ...
    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult handleMyFormSubmit(myFormModel model) {...}
    ...
  }
}

〜/查看/ myProject的/部分/ myPartialView.cshtml

~/Views/myProject/Partial/myPartialView.cshtml

@model myFormModel
@{      
  using (Html.BeginUmbracoForm("handleMyFormSubmit", "myFormSurface", null, new Dictionary<string, object> { { "class", "myFormStyle" }, { "id", "myFormId" } }))
  {
    ...
    @Html.TextBoxFor(x => x.firstFieldValue, new { @class = "myInputStyle", @placeholder = ViewData.ModelMetadata.Watermark })
    ...
  }
}

结果是占位符HTML标记正确显示在我的着色的网页,但尽管名称标签填写正确后,即使没有显示名称设置的装饰我的视图模型的财产是空的。

Result is that the placeholder html tag is showing up correctly on my rendered webpage but is empty though Name tag is filled up correctly, even without DisplayName decoration set on my view model's property.

的http://本地主机/ testpage

...
<input type="text" value="" placeholder="" name="firstFieldName" id="firstFieldName" class="myInputStyle">
...

我缺少的是在这里吗?我曾尝试确实是在正确的文件夹中创建两个编辑器模板(MultilineText和字符串)(〜/查看/共享/ EditorTemplates /),但我认为是我使用Html.TextBoxFor而不是的Html他们永远不会被调用。文本框...

What am I missing here ? I did try indeed to create both editor templates (MultilineText and String) in the correct folder (~/Views/Shared/EditorTemplates/) but I assume they are never called as I'm using "Html.TextBoxFor" and not "Html.TextBox"...

其他的事情,如果我从@ Html.TextBoxFor呼叫中除掉@placeholder = ViewData.ModelMetadata.Watermark,我没有对渲染网页上显示的占位符。这是很好的,通话的这一部分是明确的罚款。

Other thing, if I remove "@placeholder = ViewData.ModelMetadata.Watermark" from the @Html.TextBoxFor call, I don't have any "placeholder" displayed on the rendered webpage. Which is good, this part of the call is definitively fine.

提前感谢在这一点上任何帮助...

Thanks in advance for any help on that point...

尼古拉斯。

编辑:

样,如果我在我的模型创造更多的变量是什么。

What about if I create more variable in my model.

例如:

public string firstFieldPrompt { get { return "bla"; } set { } } 

然后

@Html.TextBoxFor(x => x.firstFieldValue, new { @class = "myInputStyle", @placeholder = x => x.FirstFieldPrompt })

推荐答案

你会得到一个空水印的原因是,在你的情况下(即不使用模板)的的ViewData 的实际上是指 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.

这篇关于ModelMetadata.Watermark和MVC视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆