How do I get Timestamp properties to be rendered when I use EditorForModel()?

1.4k views Asked by At

I have the following domain entity decorated with System.ComponentModel.DataAnnotations:

    [HiddenInput(DisplayValue=false)]        
    public int Id { get; set; }

    [Required]
    [Display(Name="Last Name")]
    public string LastName { get; set; }

    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }        

    [Timestamp]
    [HiddenInput(DisplayValue=true)]
    [ScaffoldColumn(true)]
    public byte[] Version { get; set; }

When I render my model in the view using @Html.EditorForModel() nothing is rendered for the version property. Is there anything that can force EditorForModel() method to render the byte[]?

PS: Using @Html.EditorFor(x => x.Version) works just fine.

3

There are 3 answers

0
marcind On

A byte[] is considered to be a complex type by the EditorForModel method and there currently is no way for those to be displayed. You could try adding another property to your model (typed as string for example) that would read from and write to your Version property.

1
Ely On

You could also create an EditorTemplate named Version and design the output how you see fit. add a UIHint attribute to version in order to use it.

0
Developix On

Better write your own template (Object.ascx). Copy the standard MVC Object.ascx template and change the ShouldShow Method to:

 public static bool ShouldShow(ModelMetadata metadata, ViewDataDictionary viewData) {
      return metadata.ShowForDisplay
      && metadata.ModelType != typeof(System.Data.EntityState)
      && (!metadata.IsComplexType || metadata.ModelType == typeof(System.Byte[]))
      && !viewData.TemplateInfo.Visited(metadata);
  }