Model and data annotation in C# and Vue.js

Viewed 340

Suppose I have a business object: (C#)

public class ModelA {
  public int id {get;set;}

  [Required, MyCustomAttribute(...)]
  public string str {get;set;}
}

Then I have a ModelAController: (C#)

[Route("api/[controller]")] 
[ApiController] 
public class ModelAController : Controller 
{
  // ... some contructors and readonly database ...

  [HttpPut("read")] 
  public ActionResult<ModelA> Read() {
    return db.Read<ModelA>();
  }
} 

Then I have some Vue component: (vue.js)

  import ...

  export default {
    data () { 
      return { 
      annotations: '',
      model: '',
    }; 
  }, 
  
  created: function() 
  { 
    this.values = Read('ModelA'); // reads data in json format returned by ModelAController.Read()
    this.annotation = // ?????
  }, 
}; 

My question is; From a Vue component, how do I access the annotations "Required" and "MyCustomAttribute" and others. Should I add a method in my controller to get these Attribute?

  [HttpPut("attributes")] 
  public ActionResult<Attribute[]> Attributes(string propertyName) { 
    return getAttributesOf(propertyName); 
    // getAttributesOf("str") would return Attribute[]{ RequiredAttribute, MyCustomAttribute(...)} 
  } 

Is there a better way?

0 Answers
Related