C# MVC调用属性中的资源文件
我想通过在“Oninvalid”属性中显示资源文件中的错误消息,使显示的错误消息本地化,以下是我的代码:C# MVC调用属性中的资源文件,c#,model-view-controller,C#,Model View Controller,我想通过在“Oninvalid”属性中显示资源文件中的错误消息,使显示的错误消息本地化,以下是我的代码: @Html.TextAreaFor(x => x.Message, new { @class = "form-control notifi-form-field", @name = "Message", @id = "MessageText",@oninvalid= "this.setCustomValidity('Resources.LayoutResources.RequiredF
@Html.TextAreaFor(x => x.Message, new { @class = "form-control notifi-form-field", @name = "Message", @id = "MessageText",@oninvalid= "this.setCustomValidity('Resources.LayoutResources.RequiredField')"})
Resources.LayoutResources.RequiredField
按原样显示。
这只是从服务器端代码发送到客户端代码的字符串文字:
"this.setCustomValidity('Resources.LayoutResources.RequiredField')"
对资源的引用需要由服务器端代码解释,因此它需要知道它不仅仅是一个字符串文本。例如:
$"this.setCustomValidity('{Resources.LayoutResources.RequiredField}')"
或使用较旧的语法:
"this.setCustomValidity('" + Resources.LayoutResources.RequiredField + "')"
请注意,在服务器端代码发出字符串值后,仍然存在单引号,以使生成的客户端代码有效