【发布时间】:2010-11-18 15:14:34
【问题描述】:
我在 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 的服务器端检查始终为真。
我可以让数据注释通过视图模型的属性拉出以进行这样的验证,还是应该以另一种方式这样做?
【问题讨论】:
-
如果回答了您的问题,请在此标记答案
标签: asp.net-mvc validation viewmodel data-annotations