MVC DataAnnotations Testing
-
05-07-2019 - |
Domanda
Ho il seguente codice in una classe parziale e sto usando LINQ to SQL:
[Bind(Include = "OrderId,OrderTypeId,CustomerName,Price")]
[MetadataType(typeof(OrderMetadata))]
public partial class Order
{
}
public class OrderMetadata
{
[DisplayName("Customer Name")]
[Required]
public object CustomerName { get; set; }
}
Sto cercando di scrivere un test per vedere se è richiesto "CustomerName" e sto usando questo codice che ho trovato qui: http://bradwilson.typepad.com/blog/2009/04/index.html
var propertyInfo = typeof (Order) .GetProperty (" CustomerName ");
var attributo = propertyInfo.GetCustomAttributes (typeof (RequiredAttribute), true) .Cast (). FirstOrDefault ();
L'attributoè sempre nullo.
Qualcuno può aiutare per favore?
Grazie
Davy
Soluzione
Penso che sia necessario ottenere il tipo OrderMetadata non Order
var propertyInfo = typeof(OrderMetadata).GetProperty("CustomerName");
La gentilezza,
Dan
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow