I am trying to modify the template that scaffolds a controller, to check if the model contains some property, and if so, write the c# code to assign it some value. so in my template, in the part that writes the Create actionresult (post):
[HttpPost]
[ValidateAntiForgeryToken]
<# if (UseAsync) { #>
public async Task<ActionResult> Create(<#= bindAttribute #><#= ModelTypeName #> <#= ModelVariable #>)
<# } else { #>
public ActionResult Create(<#= bindAttribute #><#= ModelTypeName #> <#= ModelVariable #>)
<# } #>
{
<#
if (THE MODEL CONTAINS A PROPERTY NAMED "creation_date")) {
#>
<#= modelVariable #>.creation_date = DateTime.Now;
<# } #>
is there a way to do it?
Ok I found answer myself, not sure it its the best solution but its working nicely:
<#
bool entityWithCreationDate = false;
foreach (PropertyMetadata property in ModelMetadata.Properties) {
if(property.PropertyName == "creation_date") { entityWithCreationDate=true; break; }
}
if(entityWithCreationDate==true) {
#>
<#= ModelVariable #>.creation_date = DateTime.Now;
<#
}
#>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.