0

I have created view model class like bellow

public class GenericFormModel
{ 
   public string SimpleInput { get; set; } 
   public string InputAddOn { get; set; } 
   public string InputTextArea { get; set; } 
   public DateRange Date { get; set; }  
}

Then I created my own create template CustomCreate.cs.t4 under rote/CodeTemplates/MvcView location. I try to create Input Razor control for DateRange property . But I can't able to read DateRange Property from CustomCreate.cs.t4 like below

<# 
foreach (PropertyMetadata property in ModelMetadata.Properties) {
if (property.Scaffold && !property.IsAutoGenerated && !property.IsReadOnly && !property.IsAssociation) {

if (property.IsPrimaryKey && IsPropertyGuid(property)) {
continue;
}

#> 
 @Html.InputFor(model => model.<#= property.PropertyName #>)
<#
}
}
 #>

Note:

My problem is ModelMetadata.Properties only return string,int,DateTime,decimal ..properies only. It did not return DateRange Property.

What mistake did I make? I am using VS2013 And MVC5.

InputFor() is My custom Html Helper

halfer
  • 19,824
  • 17
  • 99
  • 186
P John Raj
  • 537
  • 1
  • 4
  • 17
  • `ModelMetadata.Properties` is an ienumerable of ModelMetadata objects, not PropertyMetadata (from the docs https://msdn.microsoft.com/en-us/library/system.web.mvc.modelmetadata.properties(v=vs.118).aspx#P:System.Web.Mvc.ModelMetadata.Properties) can't find the docs for PropertyMetadata, don't know why the cast doesn't throw, so I can't tell you. Debug it, set a breakpoint and examine `ModelMetadata.Properties` to see what's in it. –  Dec 15 '15 at 15:09
  • Yes you are write, is any other way to do it. – P John Raj Dec 16 '15 at 11:11
  • I don't know, as it should blow up at runtime. Examine the object at runtime and see what you can get from it. –  Dec 16 '15 at 15:01

0 Answers0