razor - Input placeholder from ViewModel metadata in ASP.NET Core 1.0 -


is possible set built-in asp-for tag helper take input placeholder [display(prompt = "this placeholder")] attribute in view model.

[display(name="name", prompt = "this placeholder")]         public string name { get; set; } 

in mvc 5 able achieve adding additional logic in editor templates , checking viewdata.modelmetadata.watermark property. example:

@model string @{     dictionary<string, object> htmlattributes = new dictionary<string, object>();     htmlattributes.add("class", "form-control");     if (!string.isnullorwhitespace(viewdata.modelmetadata.watermark))     {         htmlattributes.add("placeholder", viewdata.modelmetadata.watermark);     } } @html.label("") @html.textbox("", viewdata.templateinfo.formattedmodelvalue, htmlattributes) @html.validationmessage("", "", new { @class = "text-danger" }) 

but in asp.net core 1.0 start using new tag helpers approach. default, build in asp-for helper ignores prompt attribute value.

having own custom implementation of asp-for attribute, e.g. my-asp-for option , end having sake of maintainability , reusing additional logic. watermark property no longer present in modelmetadata, i.e. there no such thing for.metadata.watermark.

the place can see prompt value in asp-for helper hidden.

enter image description here

so have 2 questions. there smarter way ? bad practice take placeholder viewmodel metadata, i.e. should explicitly specify it in razor file ?

i have done quick test , if understand correctly, appears work out of box me default vs template:

model:

public class loginviewmodel {     [required]     [emailaddress]     [display(name = "email", prompt = "example@example.org")]     public string email { get; set; }      [required]     [datatype(datatype.password)]     public string password { get; set; }      [display(name = "remember me?")]     public bool rememberme { get; set; } } 

view snippet:

<div class="form-group">     <label asp-for="email" class="col-md-2 control-label"></label>     <div class="col-md-10">         <input asp-for="email" class="form-control" />         <span asp-validation-for="email" class="text-danger"></span>     </div> </div> 

result:

enter image description here


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -