如何启用或禁用实体验证
我使用 Entity Framework 4 和 MVC 2。
我有地址实体、联系人、公司。
联系人和地址以及公司和地址之间存在关系。联系人可以有地址,公司也可以有地址。
我创建了地址的部分视图。
<div class="editor">
<%: Html.HiddenFor(model => model.AddressID) %>
<%: Html.HiddenFor(model => model.AddressID) %>
<div class="editor-label">
<%: Html.LabelFor(model => model.CivicNumber) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.CivicNumber)%>
<%: Html.ValidationMessageFor(model => model.CivicNumber)%>
</div>
</div>
<div class="editor">
<div class="editor-label">
<%: Html.LabelFor(model => model.Street) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Street)%>
<%: Html.ValidationMessageFor(model => model.Street)%>
</div>
</div> ......
我使用 EditorFor 在“联系人”和“公司”视图中将此部分视图称为“部分”视图。
在地址类中,我进行了一些验证。
示例:需要提供公民编号。
[Required(ErrorMessage = "Civic Number is Required")]
[DisplayName("Civic Number")]
public object CivicNumber { get; set; }
仅当我从联系人中呼叫地址时,是否可以激活此验证。换句话说。如果我从公司视图调用 Address.ascx,我不希望对地址字段进行任何验证。如果我从联系人视图调用 Address.ascx,我需要验证地址字段。
希望有人能理解。
谢谢
I use Entity Framework 4 and MVC 2.
I Have an Address Entity, Contact, Company.
There are a relation between Contact and Address and Company and Address.. A Contact can Have an Address and a Company can also have an address too.
I created a Partial View for Address.
<div class="editor">
<%: Html.HiddenFor(model => model.AddressID) %>
<%: Html.HiddenFor(model => model.AddressID) %>
<div class="editor-label">
<%: Html.LabelFor(model => model.CivicNumber) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.CivicNumber)%>
<%: Html.ValidationMessageFor(model => model.CivicNumber)%>
</div>
</div>
<div class="editor">
<div class="editor-label">
<%: Html.LabelFor(model => model.Street) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Street)%>
<%: Html.ValidationMessageFor(model => model.Street)%>
</div>
</div> ......
I Call this Partial view in my Contact and Company View with EditorFor.
In the Address Class, I had some validation.
Example : The Civic Number is required.
[Required(ErrorMessage = "Civic Number is Required")]
[DisplayName("Civic Number")]
public object CivicNumber { get; set; }
Is it possible to Active this validation only when I Call Address from Contact. In other words. If I Call the Address.ascx from Company View I Don't want any validation for Address Fields. If I Call the Address.ascx from Contact View, I want the validation for Address Fields.
Hope someone will understand.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定,但试一试。
在您的部分视图中,
您正在做的是获取父视图的模型。然后只需检查它的类型。
I am not completely sure but give it a shot.
In your partial view
What you are doing is getting the Model of the parent view. Then just check it's type.