t4 - modify a TT template to add required html element -


i trying create t4 template speed create form template.

is possible add html depending if model's property required? e.g.

  [required]   [display(name = "contact email address:")]   public string contactemailaddress { get; set; } 

now in tt file like

foreach (modelproperty property in getmodelproperties(mvchost.viewdatatype)) { if (!property.isprimarykey && !property.isreadonly) {  #>     <div>         @html.labelfor(model => model.<#= property.name #>)         @html.editorfor(model => model.<#= property.name #>)         @html.validationmessagefor(model => model.<#= property.name #>)         if(this.required==true){<span class="required-field"></span>}     </div> <# } 

or not possible?

1,open file:

c:\program files (x86)\microsoft visual studio 12.0\common7\ide\extensions\microsoft\web\mvc\scaffolding\templates\mvcviewwithcontextscaffolder\modelpropertyfunctions.include.t4 

2, add property modelproperty

class modelproperty {     public string name { get; set; }     public string associationname { get; set; }     public string valueexpression { get; set; }     public string modelvalueexpression { get; set; }     public string itemvalueexpression { get; set; }     public envdte.codetyperef type { get; set; }     public bool isprimarykey { get; set; }     public bool isforeignkey { get; set; }//     public bool isreadonly { get; set; }//     public bool isrequired{ get; set;}//here customer property     public bool scaffold { get; set; } } 

3, add method out under class

bool isrequired(envdte.codeproperty propertytype) {     foreach (envdte.codeattribute attribute in propertytype.attributes)      {         if (string.equals(attribute.fullname, "system.componentmodel.dataannotations.requiredattribute", stringcomparison.ordinal))         {             return true;         }     }     return false; } 

4,go bottom of file modify method geteligibleproperties:

list<modelproperty> geteligibleproperties(envdte.codetype typeinfo) {     list<modelproperty> results = new list<modelproperty>();     if (typeinfo != null) {         foreach (var prop in typeinfo.getpublicmembers().oftype<envdte.codeproperty>()) {             if (prop.haspublicgetter() && !prop.isindexerproperty() && isbindabletype(prop.type)) {                 string valueexpression = getvalueexpressionsuffix(prop);                  results.add(new modelproperty {                     name = prop.name,                     associationname = getassociationname(prop),                     valueexpression = valueexpression,                     modelvalueexpression = "model." + valueexpression,                     itemvalueexpression = "item." + valueexpression,                     type = prop.type,                     isprimarykey = isprimarykey(prop),                     isforeignkey = isforeignkey(prop),                     isrequired=isrequired(prop),//here customer property.                     isreadonly = !prop.haspublicsetter(),                     scaffold = scaffold(prop)                 });             }         }     }      return results; } 

5, go file

c:\program files (x86)\microsoft visual studio 12.0\common7\ide\extensions\microsoft\web\mvc\scaffolding\templates\mvcviewwithcontextscaffolder\edit.cs.t4 

add following check before validationmessagefor

@html.validationmessagefor(model => model.<#= property.name #>): 

codes this:

<#             if (property.isrequired) { #>     *<!--your html code--> <#             } #> 

Comments

Popular posts from this blog

Javascript line number mapping -

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -