如何验证 ASP.NET MVC 复杂实体框架模型中的各个集合项?
可能的重复:
嵌套(集合)属性的 mvc 客户端验证
我有一个视图显示字段形成一个复杂的实体框架模型,其中包含一些 EntityCollection 对象。事实证明它们是非常有问题的,因为 Set 方法在模型绑定期间不起作用,这在 这个 stackoverflow 问题。
现在,我在使用 EntityCollection 中 TEntity 类型上的 [Required] ValidationAttribute 元数据进行必填字段验证时遇到问题。我认为它的起源是因为当您对集合中的单个项目使用强类型 Html Helpers 时,例如 Html.TextBoxFor(model => model.Application.References.ElementAt(i).FullName)
(其中我是 for 循环中使用的 int),渲染输出的输入为 name="FullName"
而不是 name="Application.References[0].FullName"
。这根本没有将元数据应用于 References 类的属性。所以我的解决方案是使用常规的 html 帮助程序,如下所示: @Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName)< /代码>。
现在,只有 [Required] 属性创建的服务器端验证有效。我获得客户端验证的唯一方法是手动将 data-val 和 data-val-required 属性添加到呈现的输入,如下所示: @Html.TextBox("Application.References[" + i + "] .FullName", Model.Application.References.ElementAt(i).FullName, new { data_val="true", data_val_required="引用的全名“ + i + ”是必需的。"})
。对我来说,这似乎有太多的代码味道。当然有更好的方法来做到这一点吗?
以下是我的代码的详细摘录:
EF Model
public class Application
{
//...EF framework code...
public EntityCollection<Reference> References
{
//get, set
}
}
public partial class Reference : EntityObject
{
public global::System.String FullName
{
//get, set
}
}
ReferenceMetaData.cs
[MetadataType(typeof(ReferenceMetadata))]
public partial class Reference
{
}
public class ReferenceMetadata
{
[Required]
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Full Name")]
public string FullName { get; set; }
//...more properites
}
View
@for (int i = 0; i < 3; i++)
{
@Html.ValidationMessage("Application.References[" + i + "].FullName")
<div class="input-container">
<div class="editor-label">
@Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
@Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { maxlength = "100" })
</div>
</div>
@* more view code...*@
}
此代码没有手动设置 data-val 和 data-val - @Html.TextBox 上必需的属性。添加如下内容可以启用客户端验证,但代价是不共享与默认相同的验证消息(或我可能在 [Required(ErrorMessage = "Custom message")]
元数据中指定的任何消息):
@for (int i = 0; i < 3; i++)
{
@Html.ValidationMessage("Application.References[" + i + "].FullName")
<div class="input-container">
<div class="editor-label">
@Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
@Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { maxlength = "100", data_val="true", data_val_required="The Full Name for Reference " + i + " is required." })
</div>
</div>
@* more view code...*@
}
Possible Duplicate:
mvc clientside validation for nested (collection) properties
I have a view displaying fields form a complex Entity Framework model with a few EntityCollection objects in it. They've proven to be very problematic because the Set method doesn't work during model binding, which is described (and my solution provided in the answers) in this stackoverflow question.
Now I'm having a problem with required field validation using the [Required] ValidationAttribute metadata on the TEntity type in the EntityCollection. I think it originated because when you use strongly typed Html Helpers for individual items in a collection like Html.TextBoxFor(model => model.Application.References.ElementAt(i).FullName)
(where i is the int used in a for loop), the rendered output has an input with name="FullName"
rather than name="Application.References[0].FullName"
. That wasn't applying the metadata on the References class's properties at all. So my solution was to use the regular html helpers like so: @Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName)
.
Now, only the server side validation created by the [Required] attributes works. The only way I can get client side validation is to manually add the data-val and data-val-required attributes to the rendered input as follows: @Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { data_val="true", data_val_required="The Full Name for Reference " + i + " is required."})
. That seems like too much code-smell to me. Surely there's a better way to do this?
Here are excerpts from my code for details:
EF Model
public class Application
{
//...EF framework code...
public EntityCollection<Reference> References
{
//get, set
}
}
public partial class Reference : EntityObject
{
public global::System.String FullName
{
//get, set
}
}
ReferenceMetaData.cs
[MetadataType(typeof(ReferenceMetadata))]
public partial class Reference
{
}
public class ReferenceMetadata
{
[Required]
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Full Name")]
public string FullName { get; set; }
//...more properites
}
View
@for (int i = 0; i < 3; i++)
{
@Html.ValidationMessage("Application.References[" + i + "].FullName")
<div class="input-container">
<div class="editor-label">
@Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
@Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { maxlength = "100" })
</div>
</div>
@* more view code...*@
}
This code is without manually setting the data-val and data-val-required attributes on the @Html.TextBox. Adding that as follows enables client side validation, but at the cost of not sharing the same validation message as the default (or any I might specify in the [Required(ErrorMessage = "Custom message")]
metadata):
@for (int i = 0; i < 3; i++)
{
@Html.ValidationMessage("Application.References[" + i + "].FullName")
<div class="input-container">
<div class="editor-label">
@Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
@Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { maxlength = "100", data_val="true", data_val_required="The Full Name for Reference " + i + " is required." })
</div>
</div>
@* more view code...*@
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信我不小心复制了 嵌套(集合)属性的 mvc 客户端验证< /a>,该问题的答案对我来说非常有效。
I believe I accidentally made a duplicate of mvc clientside validation for nested (collection) properties, the answer provided to that question worked perfectly for me.