我一直在尝试将 DisplayAttribute 附加到 Dotnet Core 中好友类中的字段,但它没有出现在我的视图中。 例如,在视图中显示"标题"而不是"عنوان"。
这两个类通过 ModelMetadataType 链接。
哪里有问题?
原始博客类:
namespace KuteCore.Models
{
public partial class Blogs
{
public int BlogId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
博客元数据类:
namespace KuteCore.Models.MetaData
{
public class BlogsMetadata
{
[Display(Name ="عنوان")]
[Required(ErrorMessage ="خطااا")]
public string Title { get; set; }
[Display(Name = "توضیحات")]
public string Description { get; set; }
}
}
namespace KuteCore.Models.MetaData
{
[ModelMetadataType(typeof(CategoryMetadata))]
public partial class Category
{
}
}
这是我的观点
@model KuteCore.Models.Blogs
<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
我找到了问题所在。我将元数据类放在一个命名空间中,问题已解决
namespace KuteCore.Models
{
[ModelMetadataType(typeof(BlogsMetadata))]
public partial class Blogs { }
public class BlogsMetadata
{
[Display(Name ="عنوان")]
[Required(ErrorMessage ="خطااا")]
public string Title { get; set; }
[Display(Name = "توضیحات")]
public string Description { get; set; }
}
}