MVC自定义显示属性 [英] MVC custom display attribute

查看:44
本文介绍了MVC自定义显示属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为DisplayName编写了如下自定义属性.

I have written custom attribute for DisplayName as follows.

namespace CRM.Model
{
    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        private readonly string resourcekey;

        public LocalizedDisplayNameAttribute(string resourceId)
            : base(GetMessageFromResource(resourceId))
        { 

        }

        public static string GetMessageFromResource(string resourceId)
        {
            // My Localization logic
            return LocalizationResourceProvider.Current.GetString(resourceId);
        }       
    }
}

首次加载/启动模型时,上述自定义属性类被成功调用.

When model is loaded/initiated for the first time the above custom attribute class is called successfully.

但是,如果我将页面发回,则不会被调用.

But if, I will post back the page then, it doesn't get called.

例如,当我将语言从英语更改为法语时.它仍然显示英语内容.

For example when I change my language from English to French. it still displays the English content.

为什么会这样呢?我该如何解决这个问题?我们可以显式加载模型数据吗?

Why it is so ? How can I solve this issue ? Can we load the Model data explicitly

推荐答案

尝试在应用程序启动时注册属性,

Try to register your attribute on application start up, see similar thread.

这篇关于MVC自定义显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆