xVal 和 ViewModel 模式 - 可以做到吗?

发布于 2024-07-20 05:40:24 字数 1463 浏览 3 评论 0原文

我已经将 xVal 添加到 NerdDinner 应用程序中 - 到目前为止一切顺利,我在一行中使用 jQuery.validate 进行了客户端验证,这确实很漂亮。 但我似乎无法让 xVal 来验证复杂的对象。 假设我有一个看起来像这样的晚餐对象:

public class Dinner
{
     [Required]
     public string Title { get; set; }
}

和另一个对象,一个容器:

public class DinnerWrapper
{
     public Dinner Dinner { get; set; }
     public string Name { get; set; }
}

如果我的控制器将 Dinner 传递给视图,我可以让 xVal 在表单末尾执行客户端验证,像这样:

<% using (Html.BeginForm())
       { %>
    <fieldset>
        <p>
            <label for="Title">
                Dinner Title:</label>
            <%= Html.TextBox("Title") %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
    </fieldset>
    <% } %>
<%=Html.ClientSideValidation<Dinner>()%>

但是当我传递 DiningWrapper 时,我无法让它工作 - xVal 不会使用以下设置执行客户端验证:

<% using (Html.BeginForm())
       { %>
    <fieldset>
        <p>
            <label for="Title">
                Dinner Title:</label>
            <%= Html.TextBox("Title", Model.Dinner.Title) %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
    </fieldset>
    <% } %>
<%=Html.ClientSideValidation<DinnerWrapper>()%>

有什么想法吗? 到目前为止,我已经成功地将 xVal(和 NHaml)集成到 NerdDinner 应用程序中,但我似乎遇到了障碍。

I've been adding xVal to the NerdDinner app - so far so good, I get client-side validation with jQuery.validate in one line, which is truly beautiful. But I can't seem to get xVal to validate a complex object. Say I have a Dinner object that looks like this:

public class Dinner
{
     [Required]
     public string Title { get; set; }
}

and another object, a container:

public class DinnerWrapper
{
     public Dinner Dinner { get; set; }
     public string Name { get; set; }
}

If my controller passes Dinner to the View, I can get xVal to perform client-side validation at the end of my form, like this:

<% using (Html.BeginForm())
       { %>
    <fieldset>
        <p>
            <label for="Title">
                Dinner Title:</label>
            <%= Html.TextBox("Title") %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
    </fieldset>
    <% } %>
<%=Html.ClientSideValidation<Dinner>()%>

But I can't get it to work when I am passing DinnerWrapper - xVal doesn't perform client-side validation with the following setup:

<% using (Html.BeginForm())
       { %>
    <fieldset>
        <p>
            <label for="Title">
                Dinner Title:</label>
            <%= Html.TextBox("Title", Model.Dinner.Title) %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
    </fieldset>
    <% } %>
<%=Html.ClientSideValidation<DinnerWrapper>()%>

Any ideas? So far I've successfully integrated xVal (and NHaml) into the NerdDinner app, but I seem to have hit a roadblock.

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

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

发布评论

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

评论(1

几味少女 2024-07-27 05:40:24

事实证明我不必更改 ClientSideValidation 行 - 它的工作原理如下:

<%=Html.ClientSideValidation<Dinner>()%>

Turns out I didn't have to change the ClientSideValidation line - it works like this:

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