验证动态行

发布于 2024-10-11 02:20:09 字数 1391 浏览 3 评论 0原文

我已经构建了一个用于数据输入的网络应用程序(数据类型并不重要)。该站点的一部分允许用户动态添加数据行。对于静态输入字段,我使用 DataAnnotations 以便可以管理字段的验证。当我尝试对动态字段执行类似的操作时,验证似乎不起作用。

这是我的应用程序的(一般)设置。

[Data Input Page] -> [Partial Page for Dynamic Table] -> [Table is made up of individual rows (partial page)]

每个页面都是强类型的。

这是一个单独行、部分页面的示例:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Models.Person>" %>

<tr>
<% using (Html.BeginCollectionItem("People")) { %>
    <td class="ui-widget-content"><%= Html.TextBoxFor(model => model.Name) %></td>
    <td class="ui-widget-content"><%= Html.TextBoxFor(model => model.Age) %></td>
    <td class="ui-widget-content"><%= Html.TextBoxFor(model => model.PhoneNumber) %>  

    // [Cut] Some other, unimportant information here which allows a new row to be added.
</tr>

// This portion is not working - it does work with static fields.
<div class="validation">
    <div><%: Html.ValidationMessageFor(model => model.Name)%></div>
    <div><%: Html.ValidationMessageFor(model => model.Age)%></div>
    <div><%: Html.ValidationMessageFor(model => model.PhoneNumber)%></div>
</div>

任何有关使用 DataAnnotations 进行验证的见解(因此它与我已经在做的事情相同/相似)将不胜感激。

I have built a web application for data input (the type of data is unimportant). One portion of this site allows the user to dynamically add rows of data. For the static input fields, I use DataAnnotations so that I can manage validation of the fields. When I attempt to do a similar thing for the dynamic fields, validation does not appear to work.

Here is the (general) setup of my application.

[Data Input Page] -> [Partial Page for Dynamic Table] -> [Table is made up of individual rows (partial page)]

Each page is strongly typed.

Here is an example of an individual row, partial page:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Models.Person>" %>

<tr>
<% using (Html.BeginCollectionItem("People")) { %>
    <td class="ui-widget-content"><%= Html.TextBoxFor(model => model.Name) %></td>
    <td class="ui-widget-content"><%= Html.TextBoxFor(model => model.Age) %></td>
    <td class="ui-widget-content"><%= Html.TextBoxFor(model => model.PhoneNumber) %>  

    // [Cut] Some other, unimportant information here which allows a new row to be added.
</tr>

// This portion is not working - it does work with static fields.
<div class="validation">
    <div><%: Html.ValidationMessageFor(model => model.Name)%></div>
    <div><%: Html.ValidationMessageFor(model => model.Age)%></div>
    <div><%: Html.ValidationMessageFor(model => model.PhoneNumber)%></div>
</div>

Any insight into getting validation to work with DataAnnotations (so it is the same/similar to what I'm already doing) would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

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