ASP.NET MVC 模型/视图模型验证

发布于 2024-08-01 17:27:02 字数 1040 浏览 10 评论 0原文

我在 Linq-to-Sql 中有模型类,其中部分类标记有数据注释属性和对 xVal 的引用。

当我将视图直接绑定到模型时,一切都很好,xVal 生成的 JS 和服务器端都进行了双重检查。

我的许多视图不接受某一特定模型的输入,因此我正在设置视图模型类。 我不是公开整个模型实例,而是将允许/需要由视图设置的属性公开到模型中。

// foo model 
public class Foo {
    public string FooField { ... }
    public Bar Bar { ... }
}

// bar model, where bar is a parent relationship of foo in the db
public class Bar {
    public string BarField { ... }
}

// view model stuff
public class FooViewModel {
    private Foo foo;

    public FooViewModel() {
        foo = new Foo() { Bar = new Bar() };
    }

    public Foo Model {
        get { return foo; }
        set { foo = value; }
    }

    public string BarField { 
        get { return foo.Bar.BarField; }
        set { foo.Bar.BarField = value; }
    }

    public string ExtraViewModelField {
        get; set; 
    }
}

这种方法可以正确填充视图模型类,并且存储库可以正确填充记录。

但它根本没有通过验证。 我查看了发出的客户端代码,并且 xval 的验证数组为空。 此外,服务器端的 IsValid 检查始终为 true。

我可以让数据注释通过视图模型的属性进行验证吗?或者我应该以另一种方式执行此操作?

I have model classes in Linq-to-Sql with partial classes marked with data annotation attributes and a reference to xVal.

When I bind a view directly to a model everything works great, both the JS generated by xVal and server side double check.

Many of my views don't take input to one specific model, so I am setting up view model classes. Instead of exposing an entire model instance I expose properties into the model that I allow/need to be set by the view.

// foo model 
public class Foo {
    public string FooField { ... }
    public Bar Bar { ... }
}

// bar model, where bar is a parent relationship of foo in the db
public class Bar {
    public string BarField { ... }
}

// view model stuff
public class FooViewModel {
    private Foo foo;

    public FooViewModel() {
        foo = new Foo() { Bar = new Bar() };
    }

    public Foo Model {
        get { return foo; }
        set { foo = value; }
    }

    public string BarField { 
        get { return foo.Bar.BarField; }
        set { foo.Bar.BarField = value; }
    }

    public string ExtraViewModelField {
        get; set; 
    }
}

This approach populates the view model class correctly and the repository can populate the record correctly.

It doesn't pull through the validation at all though. I have looked at the client code emitted and the validation array is empty for xval. Additionally, the server side check for IsValid is always true.

Can I have the data annotations pull though the properties of view model for validation like this, or should I be doing this another way?

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

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

发布评论

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

评论(3

缱倦旧时光 2024-08-08 17:27:02

如果我正确地阅读了此内容,您将把 DataAnnotations 放在 linq to sql 类上,然后使用 linq to sql 类中的属性填充您的视图模型属性。

要使其与 xval 一起使用,您需要将 DataAnnotations 放在视图模型属性上。 据我从 xvals 代码中可以看出,它不会超越公共属性来获取任何验证信息(如果我在这里错了,请有人纠正我)。

如果您想让模型和视图模型之间的验证透明,您可以使用 postsharp 绑定属性,但如果您的程序很小,这可能会带来大量工作而收效甚微。

If i read this correctly you are putting the DataAnnotations on the linq to sql class then populating your viewmodel properties with the ones from you linq to sql class.

To get this to work with xval you would need to put the DataAnnotations on the view model properties. As far as i can tell from xvals code it dosen't look beyond the public properties for any validation information (someone please correct me if im wrong here).

If you wanted to make the validation transparent between your model and viewmodel you could go down the route of using postsharp to bind the attributes but this could be a lot of work for little gain if you program is small.

酷炫老祖宗 2024-08-08 17:27:02

如果您使用部分,并传入子类型,您仍然需要限定。 如下所示:

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

<% using (Html.BeginForm()) { %>

    <fieldset>
        <legend>Fields</legend>
            <%= Html.Hidden("OrderId", Model.OrderId) %>
            <%= Html.Hidden("ProductId", Model.ProductId)%>
        <p>
            <label for="Quantity">Quantity:</label>
            <%= Html.TextBox("OrderDetails.Quantity", Model.Quantity)%>
            <%= Html.ValidationMessage("OrderDetails.Quantity", "*") %>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

请注意,类型是 OrderDetails,但我仍然为其添加前缀以处理验证消息。

If you use partials, and pass in the subtype, you still need to qualify. See as follows:

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

<% using (Html.BeginForm()) { %>

    <fieldset>
        <legend>Fields</legend>
            <%= Html.Hidden("OrderId", Model.OrderId) %>
            <%= Html.Hidden("ProductId", Model.ProductId)%>
        <p>
            <label for="Quantity">Quantity:</label>
            <%= Html.TextBox("OrderDetails.Quantity", Model.Quantity)%>
            <%= Html.ValidationMessage("OrderDetails.Quantity", "*") %>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

Notice that the type is OrderDetails, but I'm still prefixing that to deal with validation messages.

梦冥 2024-08-08 17:27:02

您可以发布您的 xval 帮助程序代码和一些 Html.Helpers 吗?

它需要实体和前缀,所以我不明白为什么视图模型中的结构应该有任何区别。 比如:

<%= Html.ClientSideValidation<Foo>("Foo") %>
<%= Html.ClientSideValidation<Bar>("Foo.Bar") %>

詹姆斯

Can you post your xval helper code, and some of your Html.Helpers?

It takes the entity and the prefix, so I don't see why the structure within your view model should make any difference. Something like:

<%= Html.ClientSideValidation<Foo>("Foo") %>
<%= Html.ClientSideValidation<Bar>("Foo.Bar") %>

James

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