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

时间:2021-11-14 07:11:56

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应该工作。获取此占位符文本的正确方法是什么?

9 个解决方案

#1


32  

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) 

#2


4  

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!

它是完美和干净的!

#3


3  

The correct solution to get the Prompt value instead of the DisplayName in 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) - not double-escape the watermark text, which depending on the text and the language displayed can be a problem.

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

#4


2  

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

#5


2  

Use TextBoxFor:

使用TextBoxFor:

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

#6


1  

Use TextBoxFor like so:

像这样使用TextBoxFor:

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

#7


0  

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})

#8


-1  

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

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

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

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

#9


-1  

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)]

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

#1


32  

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) 

#2


4  

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!

它是完美和干净的!

#3


3  

The correct solution to get the Prompt value instead of the DisplayName in 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) - not double-escape the watermark text, which depending on the text and the language displayed can be a problem.

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

#4


2  

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

#5


2  

Use TextBoxFor:

使用TextBoxFor:

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

#6


1  

Use TextBoxFor like so:

像这样使用TextBoxFor:

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

#7


0  

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})

#8


-1  

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

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

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

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

#9


-1  

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)]

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