简体   繁体   English

在ASP.NET MVC Html帮助器中更新标签文本

[英]Update Label Text in ASP.NET MVC Html Helper

I have an ASP.NET MVC app. 我有一个ASP.NET MVC应用程序。 I am rendering the view with the help of Html Helpers. 我正在HTML帮助器的帮助下渲染视图。 For example, my .cshtml file looks like the following: 例如,我的.cshtml文件如下所示:

<div>
  @Html.Label(model => model.Price)
  @Html.TextPrice(model => model.Price)
</div>

The helpers are defined like the following in Extensions.cs: 辅助程序的定义类似于Extensions.cs中的以下内容:

public static MvcHtmlString Label<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string classes = "control-label")
{
  var attr = new Dictionary<string, object>();
  attr .Add("class", classes);

  return System.Web.Mvc.Html.LabelExtensions.LabelFor(html, expression, attr);
}

public static MvcHtmlString TextPrice<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, byte decimalPlaces = 2, string classes = "form-control")
{
  return Text(html, expression, classes + " decimal-" + decimalPlaces.ToString());
}

Eventually, I want to translate the labels into other languages. 最终,我想将标签翻译成其他语言。 For now, I need to do an intermediary translation. 现在,我需要进行中间翻译。 My question is, when I print a label, how do I grab the text, alter it, then use the new text for the label? 我的问题是,当我打印标签时,如何抓取文本,对其进行更改,然后将新文本用作标签? I do NOT want to add a bunch of Display attributes on my model at this time. 我现在不想在模型上添加一堆Display属性。 I just need to do a quick-and-dirty search and replace in my Label extension method. 我只需要进行快速搜索并替换为我的Label扩展方法。 However, I'm just not sure how to grab the text and update it. 但是,我只是不确定如何获取文本并进行更新。

Thank you! 谢谢!

You can get the property name from the expression yourself and then pass the translation, as an argument for the labelText parameter, to LabelExtensions.LabelFor() : 您可以自己从表达式中获取属性名称,然后将转换作为labelText参数的参数传递给LabelExtensions.LabelFor()

public static MvcHtmlString Label<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string classes = "control-label")
{
    var attr = new Dictionary<string, object>();
    attr .Add("class", classes);

    string propertyName = ((MemberExpression)expression.Body).Member.Name;
    string labelText = translate(propertyName);

    return System.Web.Mvc.Html.LabelExtensions.LabelFor(html, expression, labelText, attr);
}

You may use the DisplayName attribute at the Model. 您可以在模型上使用DisplayName属性。 Eg: 例如:

 [Display(Name="Character_FirstName", ResourceType=typeof(ClassLib1.Resources))]
 public string FirstName {get; set;}

See for example: http://haacked.com/archive/2011/07/14/model-metadata-and-validation-localization-using-conventions.aspx/ or https://stackoverflow.com/a/3877154/2298807 (related to Localization of DisplayNameAttribute ) 例如,请参阅: http : //haacked.com/archive/2011/07/14/model-metadata-and-validation-localization-using-conventions.aspx/https://stackoverflow.com/a/3877154/2298807 (与DisplayNameAttribute的本地化有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM