Model view controller MVC数据注释测试

Model view controller MVC数据注释测试,model-view-controller,validation,testing,data-annotations,Model View Controller,Validation,Testing,Data Annotations,我在一个分部类中有以下代码,我正在使用LINQ to SQL: [Bind(Include = "OrderId,OrderTypeId,CustomerName,Price")] [MetadataType(typeof(OrderMetadata))] public partial class Order { } public class OrderMetadata { [DisplayName("Customer Name")] [Required] pub

我在一个分部类中有以下代码,我正在使用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; }
}
我正在尝试编写一个测试,看看是否需要“CustomerName”,我正在使用我在这里找到的代码:

var propertyInfo=typeofOrder.GetPropertyCustomerName

var attribute=propertyInfo.GetCustomAttributestypeofRequiredAttribute,true.Cast.FirstOrDefault

属性始终为空

有人能帮忙吗

谢谢


Davy

我认为您需要获取类型Order,而不是Order

var propertyInfo = typeof(OrderMetadata).GetProperty("CustomerName");
善良


丹,我想你是对的。看不见,看不见:非常感谢,戴维