我想在剃刀视图中使用动态标签的模型,该模型在运行时设置,但基于使用字符串格式的资源文件中的字符串。
假设我有一个带有单个属性的简单模型
public class Simple
{
[Display(ResourceType = (typeof(Global)), Name = "UI_Property1")]
[Required(ErrorMessageResourceType = (typeof(Global)), ErrorMessageResourceName = "ERROR_Required")]
[StringLength(40, ErrorMessageResourceType = (typeof(Global)), ErrorMessageResourceName = "ERROR_MaxLength")]
public string Property1{ get; set; }
}
资源文件包含以下字符串
UI_Property1 {0}
ERROR_Required Field {0} is required.
ERROR_MaxLength Maximum length of {0} is {1}
我想在剃须刀视图中做这样的事情
@Html.LabelFor(m => m.Property1, "xyz", new { @class = "control-label col-sm-4" })
并且生成的视图会将字段标签显示为' xyz'和价值' xyz'也将显示在从服务器模型验证返回的验证消息中。
我一直在寻找各种方法,没有运气。我已经调查了覆盖DisplayAttribute,但这是一个密封的类。
我还查看了覆盖DisplayName属性,但是使用所需的验证消息无法正确选择。另外,我不确定如何将动态文本注入属性,我认为这需要在属性构造函数中完成。
我还看过编写自定义DataAnnotationsModelMetadataProvider,但看不到使用它来实现我想要的方法。这可能取决于我缺乏编码技巧。
' xyz' string将来自web.config文件中的一个设置,不需要在LabelFor命令中注入,但如果它更有意义,可以在其他地方注入。
如果有人能告诉我如何实现这一目标,那就太棒了。
答案 0 :(得分:0)
我找到了这篇文章
这使我得到了如下解决方案:
我在网页配置中添加了自定义部分
[self moveViewDownBy:<Height_Of_Your_Top_Banner>];
处理自定义部分的类加载要翻译的标签
static int send_signal(int data)
{
int ret;
struct siginfo info;
struct task_struct *t;
/* send the signal */
memset(&info, 0, sizeof(info));
/* I have tried 44 and 30 but both are not working */
info.si_signo = sig_num;
info.si_code = SI_QUEUE;
info.si_int = data;
if (!g_user_pid) {
printk("error seding signal, pid is not configured");
return -EAGAIN;
}
rcu_read_lock();
t = pid_task(find_pid_ns(g_user_pid, &init_pid_ns), PIDTYPE_PID);
if (t == NULL) {
printk("invalid pid\n");
rcu_read_unlock();
return -EAGAIN;
}
printk("sending value %u to pid %d\n", info.si_int, (int)t->pid);
ret = send_sig_info(sig_num, &info, t); /* send the signal */
rcu_read_unlock();
if (ret < 0) {
printk("error sending signal\n");
return ret;
}
}
然后翻译人员使用自定义部分将给定标签翻译成翻译版本(如果存在),否则返回标签
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="labelTranslations" type="AttributeTesting.Config.LabelTranslatorSection" />
... other sections here
</configSections>
<labelTranslations>
<labels>
<add label=":Customer:" translateTo="Customer Name" />
<add label=":Portfolio:" translateTo="Portfolio Name" />
<add label=":Site:" translateTo="Site Name" />
</labels>
</labelTranslations>
然后我写了一个自定义元数据提供程序,它根据翻译版本修改显示名称
public class LabelElement : ConfigurationElement
{
private const string LABEL = "label";
private const string TRANSLATE_TO = "translateTo";
[ConfigurationProperty(LABEL, IsKey = true, IsRequired = true)]
public string Label
{
get { return (string)this[LABEL]; }
set { this[LABEL] = value; }
}
[ConfigurationProperty(TRANSLATE_TO, IsRequired = true)]
public string TranslateTo
{
get { return (string)this[TRANSLATE_TO]; }
set { this[TRANSLATE_TO] = value; }
}
}
[ConfigurationCollection(typeof(LabelElement))]
public class LabelElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new LabelElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((LabelElement)element).Label;
}
public LabelElement this[string key]
{
get
{
return this.OfType<LabelElement>().FirstOrDefault(item => item.Label == key);
}
}
}
public class LabelTranslatorSection : ConfigurationSection
{
private const string LABELS = "labels";
[ConfigurationProperty(LABELS, IsDefaultCollection = true)]
public LabelElementCollection Labels
{
get { return (LabelElementCollection)this[LABELS]; }
set { this[LABELS] = value; }
}
}
之后,它只是工作,标签和验证消息都使用翻译版本。我使用标准的LabelFor助手而没有任何修改。
资源文件看起来像这样
public static class Translator
{
private readonly static LabelTranslatorSection config =
ConfigurationManager.GetSection("labelTranslations") as LabelTranslatorSection;
public static string Translate(string label)
{
return config.Labels[label] != null ? config.Labels[label].TranslateTo : label;
}
}