Thuộc tính DisplayAttribute trong System.ComponentModel.DataAnnotations có thuộc tính GroupName, cho phép bạn nhóm các trường hợp lý lại với nhau trong điều khiển giao diện người dùng (ví dụ: lưới thuộc tính trong WPF/WinForms).Cách lấy chú thích dữ liệu GroupName từ ModelMetadata
Tôi đang cố truy cập siêu dữ liệu này trong ứng dụng ASP.NET MVC3, về cơ bản là tạo lưới thuộc tính. Nếu mô hình của tôi trông như thế này:
public class Customer
{
[ReadOnly]
public int Id { get;set; }
[Display(Name = "Name", Description = "Customer's name", GroupName = "Basic")]
[Required(ErrorMessage = "Please enter the customer's name")]
[StringLength(255)]
public string Name { get;set; }
[Display(Name = "Email", Description = "Customer's primary email address", GroupName = "Basic")]
[Required]
[StringLength(255)]
[DataType(DataType.Email)]
public string EmailAddress { get;set; }
[Display(Name = "Last Order", Description = "The date when the customer last placed an order", GroupName = "Status")]
public DateTime LastOrderPlaced { get;set; }
[Display(Name = "Locked", Description = "Whether the customer account is locked", GroupName = "Status")]
public bool IsLocked { get;set; }
}
và quan điểm của tôi trông như thế này:
@model Customer
<div class="edit-customer">
@foreach (var property in ViewData.ModelMetadata.Properties.Where(p => !p.IsReadOnly).OrderBy(p => p.Order))
{
<div class="editor-row">
@Html.DevExpress().Label(settings =>
{
settings.AssociatedControlName = property.PropertyName;
settings.Text = property.DisplayName;
settings.ToolTip = property.Description;
}).GetHtml()
<span class="editor-field">
@Html.DevExpress().TextBox(settings =>
{
settings.Name = property.PropertyName;
settings.Properties.NullText = property.Watermark;
settings.Width = 200;
settings.Properties.ValidationSettings.RequiredField.IsRequired = property.IsRequired;
settings.ShowModelErrors = true;
}).Bind(ViewData[property.PropertyName]).GetHtml()
</span>
</div>
}
</div>
thì mẫu được đặt ra rất độc đáo dựa trên siêu dữ liệu, với các nhãn, tooltips, watermarks vv tất cả kéo ra khỏi siêu dữ liệu của mô hình; nhưng, tôi muốn nhóm các mục lại với nhau, ví dụ: <fieldset>
cho mỗi nhóm. Có ai biết làm thế nào để có được GroupName ra khỏi siêu dữ liệu, viết tắt của một phương pháp mở rộng cho ModelMetadata?
Cảm ơn, điều đó có vẻ giống như những gì tôi cần. –