如何通过在模型类上使用Display(prompt)属性在脚手架视图中获得HTML输入中的占位符文本



我使用脚手架为我生成视图和控制器,我使用EF代码优先语法

我理解T4模板负责在生成的HTML/视图代码中实现属性值,但我没有看到VS 2015 community edition可用的默认脚手架模板为占位符文本做任何事情。

根据我的理解,在用[Display(Prompt="some placeholder text")]属性装饰模型属性时,some placeholder text被显示为输入文本框的占位符创建/编辑视图

但令我沮丧的是,这并没有发生。

还有其他属性吗?还是其他我需要做的事?还是因为我使用脚手架来生成视图?还是默认的T4模板没有很好地完成它的工作?

模型类的代码如下:

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

但我想控制所有的东西从模型,并希望使用脚手架。最好甚至可以编辑/自定义T4模板来完成此操作。

DisplayAttributePrompt属性将值添加到与该属性关联的ModelMetadataWaterMark属性上。它不用于生成html placeholder属性。

你想使用它的值作为placeholder属性,但另一个开发人员可能想使用它来生成一个工具提示(使用title属性),所以它留给开发人员他们可能想要如何使用它。

脚手架编辑视图的T4模板为模型的属性生成@Html.Editor(),因此您可以在/Views/Shared/EditorTemplates文件夹中创建自己的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; }

你可以试试这个

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)

最新更新