asp.net-mvc ASP.NET MVC TextBoxFor 占位符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12898471/
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-09-08 02:20:42  来源:igfitidea点击:

ASP.NET MVC TextBoxFor Placeholder

asp.net-mvc

提问by Subby

I have the following textbox in my View:

我的 中有以下文本框View

enter image description here

在此处输入图片说明

The code for the placeholder:

占位符的代码:

@Html.TextBoxFor(m => m.Listing.Location.AddressLine1, new {@placeholder="Address Line 1 - Required"})

Question:How do I make the text "Required" in RED.

问题:如何使文本“必需”为红色。

回答by Darin Dimitrov

You can't do this with a text field. You have no control over the formatting of only some portions of the HTML5 placeholderattribute. Not to mention that if you want to achieve multicolor text you can no longer use a text field or textarea. You will have to use an element with contenteditable="true"attribute and write your custom plugin.

您不能使用文本字段执行此操作。您无法控制 HTML5placeholder属性的某些部分的格式。更不用说如果你想实现多色文本,你不能再使用文本字段或文本区域。您必须使用具有contenteditable="true"属性的元素并编写自定义插件。

Here's for example how the markup might look like:

例如,标记可能如下所示:

<div contenteditable="true">
    Address Line 1 - <span style="color: red">Required</span>;
</div>

But since this is a divelement you will now have to back it up with a corresponding hidden field so that the value is sent to the server. Also when the value of this field changes you need to resynchronize the hidden field value as well. It will be a lot of work.

但由于这是一个div元素,您现在必须使用相应的隐藏字段备份它,以便将值发送到服务器。此外,当该字段的值发生变化时,您还需要重新同步隐藏字段的值。这将是很多工作。

回答by Mizanur Rahman

in model

在模型中

[Required]

[必需的]

public string AddressLine1{ get; set; }

@Html.TextBoxFor(model => model.Listing.Location.AddressLine1, new { @class = "form-control" , placeholder = "Address Line 1 - Required" })

try this for an empty @Html.TextBox

试试这个空的@Html.TextBox

@Html.TextBox("CustomarName" ,null, new { @class = "form-control" , @placeholder = "Search With Customar Name" })