如何启用或禁用实体验证

发布于 2024-09-25 10:29:05 字数 1463 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2024-10-02 10:29:05

我不完全确定,但试一试。

在您的部分视图中,

<% bool outputValidation = this.ViewContext.Controller.ViewData.Model.GetType() == typeof(Contact) %>

<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)%>
        <%: outputValidation ? Html.ValidationMessageFor(model => model.CivicNumber) : "" %>
    </div>
</div>

您正在做的是获取父视图的模型。然后只需检查它的类型。

I am not completely sure but give it a shot.

In your partial view

<% bool outputValidation = this.ViewContext.Controller.ViewData.Model.GetType() == typeof(Contact) %>

<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)%>
        <%: outputValidation ? Html.ValidationMessageFor(model => model.CivicNumber) : "" %>
    </div>
</div>

What you are doing is getting the Model of the parent view. Then just check it's type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文