Html 如何将模型中的占位符文本添加到 MVC 视图中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22105833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:05:30  来源:igfitidea点击:

How do I add placeholder text from the model into a MVC view?

asp.net-mvchtml

提问by John S

I have a model:

我有一个模型:

[DataType(DataType.EmailAddress)]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[Display(Prompt = "Email Address")]
public string Email { get; set; }

I am trying to get the "prompt" to show in the placeholder text of the resulting text box with the following:

我试图让“提示”显示在结果文本框的占位符文本中,如下所示:

@Html.EditorFor(model => model.Email, 
new { htmlAttributes = new { @class = "form-control input-md",
placeholder = @ViewData.ModelMetadata.Watermark } })

When I view the generated HTML I only get "placeholder" in the input tag. According to what I have read ViewData.ModelMetadata.Watermark should work. What is the correct way to get this placeholder text in place?

当我查看生成的 HTML 时,我只在输入标签中得到“占位符”。根据我所读的 ViewData.ModelMetadata.Watermark 应该可以工作。获取此占位符文本的正确方法是什么?

回答by John S

This solved my issue:

这解决了我的问题:

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

The code that did it was

这样做的代码是

placeholder = @Html.DisplayNameFor(m=>m.Email) 

回答by The girl with red hair

A little late, but if someone is still looking for it...

有点晚了,但如果有人还在寻找它......

@Html.EditorFor(model => model.Email, 
new { htmlAttributes = new { @class = "form-control input-md",
@placeholder = "Whatever you want as a placeholder" } })

It's perfect and clean!

它完美而干净!

回答by marapet

The correct solution to get the Promptvalue instead of the DisplayNamein a non-templated control context is the following:

在非模板化控件上下文中获取Prompt值而不是的正确解决方案DisplayName如下:

@Html.EditorFor(model => model.Email, 
    new { @class = "form-control input-md",
    placeholder = ModelMetadata.FromLambdaExpression(m => m.Email, ViewData).Watermark
})

This will also - unlike the accepted solution using @Html.DisplayNameFor(m=>m.Email)- notdouble-escape the watermark text, which depending on the text and the language displayed can be a problem.

这也将 - 与使用的公认解决方案不同@Html.DisplayNameFor(m=>m.Email)-不会双重转义水印文本,这取决于所显示的文本和语言可能是一个问题。

回答by Shalom Dahan

Use TextBoxFor:

使用 TextBoxFor:

@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Email) })

回答by Muhammad Bilal

@Html.TextBoxFor(m => m.Email, new { @class = "form-control" , @placeholder = "Username" })

回答by dc922

Use TextBoxFor like so:

像这样使用 TextBoxFor:

 @Html.TextBoxFor(model => model.LastName, new{placeholder="Last Name"})

回答by CSharper

In your controller method that renders view say

在呈现视图的控制器方法中说

ViewData["Name"] = "blabbity blah";

ViewData["Name"] = "blabbity blah";

then

然后

@Html.TextBoxFor(u => u.Company, new { @placeholder = @ViewData["Name"] })

@Html.TextBoxFor(u => u.Company, new { @placeholder = @ViewData["Name"] })

Actually better yet you can do this.

实际上更好,但你可以这样做。

 public ActionResult Index()
    {              
          NewLogin n = new ClassModel().PopModel();
          n.Company = "fsdfsdf";

          return View(n);
    }

@Html.TextBoxFor(u => u.Company, new { @placeholder = Model.Company })

@Html.TextBoxFor(u => u.Company, new { @placeholder = Model.Company })

回答by Prabu

Check out this answers to this question, which has been answered aleady (a few times).

看看这个问题的答案,已经回答了aleady(几次)。

@Html.EditorForuses templates, and so the approach to this problem is using a customised template.

@Html.EditorFor使用模板,因此解决此问题的方法是使用自定义模板。

回答by Sabin Kumar Sharma

you can use it as follows for the date which will also give you a "datetime" picker.

您可以按如下方式使用它作为日期,这也将为您提供“日期时间”选择器。

[DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}",ApplyFormatInEditMode =true)]