0

There is the MetadataTypeAttribute to specify the metadata class to associate with a data model class. So, I can define metadata, that help render HTML controls in an MVC application, within an associated class from another file. For example:

User.cs

[MetadataType(typeof(UserMetaData))]
public partial class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
}

MetaData.cs

public partial class UserMetaData
{
    [HiddenInput(DisplayValue = false)]
    public int UserId { get; set; }
    [DisplayName("Your Name")]
    public string FirstName { get; set; }
    [DisplayName("Your Last Name")]
    public string LastName { get; set; }
}

Is there similar way to define JsonProperty attributes for my model's properties in another file?

Alexander
  • 4,420
  • 7
  • 27
  • 42
  • 1
    Have you tried it? What was the result? Json.NET does support `[MetadataType]`, e.g. see [here](https://stackoverflow.com/a/45045881/3744182) and [here](https://stackoverflow.com/a/10306177/3744182) for examples of the two being used in tandem. – dbc Aug 08 '17 at 18:13
  • 1
    In fact, it just works. See https://dotnetfiddle.net/bUusfe – dbc Aug 08 '17 at 18:26
  • @dbc, is the answer so obvious?! Thank you! – Alexander Aug 09 '17 at 03:02

0 Answers0