Search code examples
.net-coredisplayattributebuddy-class

DisplayAttribute not work in Dotnet Core buddy class


I've been trying to attach a DisplayAttribute to a field in my buddy class in Dotnet Core, but it not appears in my View. for example in View display "Title" instead "عنوان".

The two classes are linked via ModelMetadataType.

where is wrong?

orginal blogs class:

namespace KuteCore.Models
{
    public partial class Blogs
    {
        public int BlogId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
    }
}

BlogsMetadata class:

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
    {
    }
}

and this my View

@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>

Solution

  • I found where the problem was. I put Metadata classes in a One namespace and the problem was fixed

    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; }
        }
    }