1

我一直在尝试将 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>
4

1 回答 1

1

我发现问题出在哪里。我将元数据类放在 One 命名空间中,问题已解决

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; }
    }
}
于 2019-11-29T09:55:29.903 回答