asp.net-mvcentity-framework-4knockout.jsknockout-2.0upshot

How does one access and bind the entity meta-data (through JavaScript on the client) available from Upshot?


One of the supposed benefits of Upshot is its ability to query meta-data exposed through Data Annotations. How would one access meta-data for entity properties for things like string length, default values, display name, description information, and whether a field is required?

How would connect this data into a validation framwork like jQuery Validation or Knockout Validation?

Note: I am currently using Knockout 2.1.0, Upshot 1.0.0.2, Entity Framework 4.3, and ASP.NET MVC4.


Solution

  • You can use the upshot function metadata() to access this information, for example:

    If you have the following upshot registration:

    @(Html.UpshotContext(bufferChanges: true)
        .DataSource<App.Controllers.DBController>(x => x.GetUsers())
        .ClientMapping<App.Models.User>("User")
    )
    

    you can access the metadata (and validation rules) like this:

    var metadata = upshot.metadata(upshot.type(User));
    

    you can also retrieve information about properties (eg. "Name"):

    var propertyType = upshot.metadata.getPropertyType(upshot.type(User), "Name");