C# 带有ValidationMessage的复杂类型
我有一个项目需要一些内部化。具体来说,我有一个名为“LocalizedString”的类,它保存特定文本的英语和德语翻译 这看起来像这样:C# 带有ValidationMessage的复杂类型,c#,validation,asp.net-mvc-5,C#,Validation,Asp.net Mvc 5,我有一个项目需要一些内部化。具体来说,我有一个名为“LocalizedString”的类,它保存特定文本的英语和德语翻译 这看起来像这样: [ComplexType] public class LocalizedString : IComparer, IComparable { public string EnglishText { get; set; } public string GermanText { get; set; } // this is only an exa
[ComplexType]
public class LocalizedString : IComparer, IComparable
{
public string EnglishText { get; set; }
public string GermanText { get; set; }
// this is only an example - the real class has some methods to return the text in the current language.
}
public class DemoItem
{
public LocalizedString ItemDescription {get; set;}
}
@model Domain.Entities.DemoItem
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DemoItem</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ItemDescription , htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ItemDescription , new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ItemDescription , "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
该类几乎用于我的所有域和视图模型,如下所示:
[ComplexType]
public class LocalizedString : IComparer, IComparable
{
public string EnglishText { get; set; }
public string GermanText { get; set; }
// this is only an example - the real class has some methods to return the text in the current language.
}
public class DemoItem
{
public LocalizedString ItemDescription {get; set;}
}
@model Domain.Entities.DemoItem
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DemoItem</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ItemDescription , htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ItemDescription , new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ItemDescription , "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
最后,DemoItem的呈现方式如下:
[ComplexType]
public class LocalizedString : IComparer, IComparable
{
public string EnglishText { get; set; }
public string GermanText { get; set; }
// this is only an example - the real class has some methods to return the text in the current language.
}
public class DemoItem
{
public LocalizedString ItemDescription {get; set;}
}
@model Domain.Entities.DemoItem
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DemoItem</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ItemDescription , htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ItemDescription , new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ItemDescription , "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model Domain.Entities.DemoItem
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
演示项目
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.itemsdescription,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.itemsdescription,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.itemsdescription,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
现在的问题是,EditorFor方法将两个文本框呈现为ItemDescription的输入字段,这非常好,应该是这样的。但是如果出现错误,例如用户忘记输入德语描述,ValidationMessageFor()将不起作用。或者更具体地说:不会向用户显示错误,因为回发提供的项目不是预期的格式。通过ValidationSummary显示所有错误是可行的,但不如在有问题的元素旁边显示错误那么好
是否有一种简单的方法可以获取特定于有问题元素的ValidationMessages?如果在LocalizedString类中为属性使用了DataAnnotation属性,则验证消息将显示在有问题元素的旁边 我向GermanText和EnglishText添加了验证属性,如下所示
[Required]
public string EnglishText { get; set; }
[Required]
public string GermanText { get; set; }
并且能够看到违规元素旁边的验证消息。通过这样做,我可以在每个有问题的元素旁边看到验证消息
我希望这会有帮助 为什么不使用字符串资源和Asp.net MVC内置的本地化功能。因为文本可以(也将)由用户提供。所有静态/管理的内容都在resx文件中,但某些部分在运行时是未知的。我建立这个解决方案后的指导,所以。。。