我正在用C#构建一个MVC Web应用程序.由于该网站将是多语言的,我已经实现了自己的ResourceManager.该类负责从数据库/缓存中获取所需的资源字符串,具体取决于当前的线程文化,并且到目前为止工作正常.
我的问题是,我想使用我的自定义ResourceManager解决方案来获取验证错误消息,例如在属性上使用必需属性.可以这样做吗?
在MVC的世界里,我有这个视图模型......
public class MyViewModel{
[Required]
public string FirstName{ get; set; } }
Run Code Online (Sandbox Code Playgroud)
......在我看来......这种事情......
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<%= Html.TextBox("FirstName") %>
<%= Html.ValidationMessage("FirstName", "*") %>
Run Code Online (Sandbox Code Playgroud)
我的问题:如果我提交此表单而不提供名称,我会收到以下消息"FirstName字段是必需的"
好.所以,我去改变我的财产......
[DisplayName("First Name")]
[Required]
public string FirstName{ get; set; }
Run Code Online (Sandbox Code Playgroud)
..现在得到"名字字段是必需的"
到目前为止都很好.
所以现在我想要错误信息显示"名字Blah Blah".如何覆盖默认消息以显示DisplayName +"Blah Blah",而不是用类似的东西注释所有属性
[Required(ErrorMessage = "First Name Blah Blah")]
Run Code Online (Sandbox Code Playgroud)
干杯,
ETFairfax
我对Asp.Net MVC很天真.
我有一个局部视图(ASP.Net MVC),其中我有一些必需的字段我想显示自定义错误消息,如果没有提供任何必填字段.以下是我的部分视图的完整cshtml代码.
@model CMSAdminPanel.ViewModel.ProductView
<h4>Material And Labour Cost For Each Size</h4>
<hr />
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
@for (int i = 0; i < Model.ServiceView.ListPriceView.Count; i++)
{
@Html.HiddenFor(x => x.ServiceView.ListPriceView[i].ProductSizeType)
<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].ProductSizeTypeName, "Size - " + Model.ServiceView.ListPriceView[i].ProductSizeTypeName, htmlAttributes: new { @class = "control-label col-md-4" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(x => x.ServiceView.ListPriceView[i].LabourCost, new { htmlAttributes = new { @class …Run Code Online (Sandbox Code Playgroud)