【问题标题】:Display placeholder text from Language resource file显示语言资源文件中的占位符文本
【发布时间】:2021-03-11 17:44:27
【问题描述】:

我有一个文本区域来显示描述和我的模型看起来一样

[Display(Name = "CopyrightHolder_AdditionalInfo_Label", ResourceType = typeof(Resource))]
public string AdditionalInfo { get; set; }

在 Razor 中,我正在使用这样的表单

<div class="form-group">
    @Html.LabelFor(model => model.AdditionalInfo)
    @Html.TextAreaFor(model => model.AdditionalInfo, new { @class = "form-control", @rows = "5", @placeholder = "i wnat to load from resource file this text", @maxlength = "545" })
    @Html.ValidationMessageFor(model => model.AdditionalInfo)
</div>

我还计划在此文本区域上显示一个占位符,该占位符应从资源文件加载 有没有办法做同样的事情?

【问题讨论】:

  • 您始终可以读取该值并将其分配给 ViewBag 或查看模型属性并使用 new { placeholder = ViewBag.PlaceHolder }(或者您是否想使用 DataAnnotation 来执行此操作?
  • ViewBag 或 Model 属性对我有用 :) 不确定 DataAnnotation。使用它有什么好处吗?
  • 这只是意味着你可以使用像[PlaceHolder(Name = "...", ResourceType = typeof(Resource)] 这样的东西,但它需要做很多工作(意味着创建你自己的实现IMetadataAwareAttribute 并创建你自己的HtmlHelper 扩展方法来生成html) - 除非您经常使用它,否则可能不值得。

标签: c# asp.net-mvc html razor localization


【解决方案1】:

你可以像下面这样添加它:

@Html.TextAreaFor(
    model => model.AdditionalInfo,
    new {
        @class = "form-control",
        @rows = "5",
        @placeholder = @Resources.Strings.YourKeyString,
        @maxlength = "545"
    }
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2014-08-08
    相关资源
    最近更新 更多