如何通过在模型 class 上使用 Display(prompt) 属性在脚手架视图中的 HTML 输入中获取占位符文本
How to get placeholder text in HTML input in a scaffolded view by using Display(prompt) attribute over model class
我使用脚手架为我生成视图和控制器并且我使用EF代码优先语法.
我知道 T4 模板负责在生成的 HTML/view 代码中实现属性值,但我没有看到 VS 2015 community edition
可用的默认脚手架模板对占位符文本做任何事情。
根据我的理解,用 [Display(Prompt="some placeholder text")]
属性装饰模型 属性 会导致 some placeholder text
显示为输入文本框的占位符
在 create/edit 观看次数中。
但令我沮丧的是,这并没有发生。
还有其他属性吗?还是我需要做的其他事情?还是因为我使用脚手架生成视图?还是默认的 T4 模板没有很好地发挥作用?
我的模型 class 代码如下所示:
public class Status
{
public int ID { get; set; }
[Required(ErrorMessage ="Status Name is needed!")]
[Display(Name ="Status Name",Prompt ="Type something here!")]
public string StatusName { get; set; }
[Required]
public string Description { get; set; }
}
下面是生成的视图代码:
@model LunchFeedback.Models.Status
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Status</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.StatusName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StatusName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
编辑:
我很清楚直接编辑视图文件并在其中添加占位符可以完成这项工作。
@Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control", placeholder = "Type something here!" } })
但是我想从模型中控制所有的东西并且想使用脚手架。最好连 edit/customize T4 模板也这样做。
你可以试试这个
public static class AppEditors
{
public static MvcHtmlString AppTextBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null )
{
var member = expression.Body as MemberExpression;
var placeholder = member.Member
.GetCustomAttributes(typeof(Display), false)
.FirstOrDefault() as Display;
var attributes = (IDictionary<string, object>)newRouteValueDictionary(htmlAttributes) ?? new RouteValueDictionary();
if (placeholder!= null)
{
attributes.Add("placeholder", placeholder.Prompt);
}
return htmlHelper.TextBoxFor(expression, attributes);
}
}
在视图中
@AppEditors.AppTextBoxFor(Html,x => x.StatusName)
DisplayAttribute
的 Prompt
属性 将值添加到与 [=45] 关联的 ModelMetadata
的 WaterMark
属性 =].它不用于生成 html placeholder
属性。
您想将它的值用作 placeholder
属性,但其他开发人员可能想使用它来生成工具提示(使用 title
属性),因此如何由开发人员决定他们可能想使用它。
脚手架编辑视图的 T4 模板为您的模型属性生成 @Html.Editor()
,因此您可以在 /Views/Shared/EditorTemplates
文件夹(或 /Views/youControllerName/EditorTemplates
如果您希望为特定控制器使用更具体的模板)
请注意,EditorFor()
方法首先在 /Views/youControllerName/EditorTemplates
中搜索模板。如果找不到,则在 /Views/Shared/EditorTemplates
中搜索,如果找不到,则使用默认值 EditorTemplate
例如,要为类型为 string
的所有属性使用模板,请在 EditorTemplates
文件夹
中创建一个名为 String.cshtml
的局部视图
@Html.TextBox("", ViewData.ModelMetadata.Model, new { placeholder = ViewData.ModelMetadata.Watermark })
或将此模板的使用限制在某些属性上,命名部分(例如)PlaceHolder.cshtml
,然后在 属性
上使用 UIHintAttribute
[Display(Name ="Status Name", Prompt ="Type something here!")]
[UIHint("PlaceHolder")]
public string StatusName { get; set; }
我使用脚手架为我生成视图和控制器并且我使用EF代码优先语法.
我知道 T4 模板负责在生成的 HTML/view 代码中实现属性值,但我没有看到 VS 2015 community edition
可用的默认脚手架模板对占位符文本做任何事情。
根据我的理解,用 [Display(Prompt="some placeholder text")]
属性装饰模型 属性 会导致 some placeholder text
显示为输入文本框的占位符
在 create/edit 观看次数中。
但令我沮丧的是,这并没有发生。
还有其他属性吗?还是我需要做的其他事情?还是因为我使用脚手架生成视图?还是默认的 T4 模板没有很好地发挥作用?
我的模型 class 代码如下所示:
public class Status
{
public int ID { get; set; }
[Required(ErrorMessage ="Status Name is needed!")]
[Display(Name ="Status Name",Prompt ="Type something here!")]
public string StatusName { get; set; }
[Required]
public string Description { get; set; }
}
下面是生成的视图代码:
@model LunchFeedback.Models.Status
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Status</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.StatusName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StatusName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
编辑:
我很清楚直接编辑视图文件并在其中添加占位符可以完成这项工作。
@Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control", placeholder = "Type something here!" } })
但是我想从模型中控制所有的东西并且想使用脚手架。最好连 edit/customize T4 模板也这样做。
你可以试试这个
public static class AppEditors
{
public static MvcHtmlString AppTextBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null )
{
var member = expression.Body as MemberExpression;
var placeholder = member.Member
.GetCustomAttributes(typeof(Display), false)
.FirstOrDefault() as Display;
var attributes = (IDictionary<string, object>)newRouteValueDictionary(htmlAttributes) ?? new RouteValueDictionary();
if (placeholder!= null)
{
attributes.Add("placeholder", placeholder.Prompt);
}
return htmlHelper.TextBoxFor(expression, attributes);
}
}
在视图中
@AppEditors.AppTextBoxFor(Html,x => x.StatusName)
DisplayAttribute
的 Prompt
属性 将值添加到与 [=45] 关联的 ModelMetadata
的 WaterMark
属性 =].它不用于生成 html placeholder
属性。
您想将它的值用作 placeholder
属性,但其他开发人员可能想使用它来生成工具提示(使用 title
属性),因此如何由开发人员决定他们可能想使用它。
脚手架编辑视图的 T4 模板为您的模型属性生成 @Html.Editor()
,因此您可以在 /Views/Shared/EditorTemplates
文件夹(或 /Views/youControllerName/EditorTemplates
如果您希望为特定控制器使用更具体的模板)
请注意,EditorFor()
方法首先在 /Views/youControllerName/EditorTemplates
中搜索模板。如果找不到,则在 /Views/Shared/EditorTemplates
中搜索,如果找不到,则使用默认值 EditorTemplate
例如,要为类型为 string
的所有属性使用模板,请在 EditorTemplates
文件夹
String.cshtml
的局部视图
@Html.TextBox("", ViewData.ModelMetadata.Model, new { placeholder = ViewData.ModelMetadata.Watermark })
或将此模板的使用限制在某些属性上,命名部分(例如)PlaceHolder.cshtml
,然后在 属性
UIHintAttribute
[Display(Name ="Status Name", Prompt ="Type something here!")]
[UIHint("PlaceHolder")]
public string StatusName { get; set; }