How to get placeholder text in HTML input as a forest using the Display attribute (prompt) above the model class

I used forests to create views and controllers for me, and I use the EF code syntax.

I understand that T4 templates are responsible for implementing attribute values ​​in the generated HTML / view code, but I don’t see the default forest template available with the VS 2015 community edition for anything for placeholder text.

In my understanding, when decorating a model property, the [Display(Prompt="some placeholder text")] attribute causes some placeholder text appear as a placeholder for the input text field in the create / edit views.

But, to my horror, this does not happen.

Is there any other attribute? or something else i need to do? or is it because i used scaffolding to create views? or does the default T4 template not do its job very well?

My code for the model class is as follows:

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

The following is the code for the generated view:

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

Edit:

I am well aware that directly editing a view file and adding a placeholder can do the job.

  @Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control", placeholder = "Type something here!" } }) 

But I want to control all the things from the model and want to use scaffolding. It is even preferable to edit / customize the T4 template for this.

+6
source share
2 answers

The Prompt DisplayAttribute property adds a value to the WaterMark property of the WaterMark object associated with this property. It is not used to generate the html placeholder attribute.

You want to use its value as a placeholder attribute, but another developer may want to use it to create a tooltip (using the title attribute), so he stayed with the developer about how they can use it.

T4 templates for editing the right forest generate @Html.Editor() for the properties of your model, so you can create your own EditorTemplates in the folder /Views/Shared/EditorTemplates (or /Views/youControllerName/EditorTemplates if you want more specific templates used for a specific controller)

Note that the EditorFor() method first searches for the template in /Views/youControllerName/EditorTemplates . If it does not find it, then it searches in /Views/Shared/EditorTemplates , and if it is not found, then it uses the default EditorTemplate

For example, to use a template for all properties of type string , create a partial view named String.cshtml in the EditorTemplates folder

 @Html.TextBox("", ViewData.ModelMetadata.Model, new { placeholder = ViewData.ModelMetadata.Watermark }) 

or restrict the use of this template to only certain properties, name partial (say) PlaceHolder.cshtml , and then use UIHintAttribute in the property

 [Display(Name ="Status Name", Prompt ="Type something here!")] [UIHint("PlaceHolder")] public string StatusName { get; set; } 
+5
source

You can try this

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

In view

  @AppEditors.AppTextBoxFor(Html,x => x.StatusName) 
+2
source

All Articles