【发布时间】:2009-08-10 19:04:12
【问题描述】:
我有一个实现 IDataErrorInfo 的验证类,它包含您必须实现的所有方法。
一个是
public string this[string columnName]
{
get
{
}
}
return "";
现在在这个 switch 语句中,我对我的表单进行了一些验证。
现在我注意到我的许多表单都需要完全相同的验证内容。
所以现在当我需要添加更多验证时,我会创建一个实现“IDataErrorInfo”的新类,添加额外的验证。
然后在视图中我做这样的事情
CoreValidation coreReg = new CoreValidation();
try
{
UpdateModel(coreReg, regForm.ToValueProvider());
}
catch
{
return View();
}
MoreValidation moreReg = new MoreValidation();
try
{
UpdateModel(moreReg , regForm.ToValueProvider());
}
catch
{
return View();
}
我只是不喜欢这样一个事实,我需要有 2 个单独的 try catch 语句,它们可以不断增长。如果我需要说“核心”和“更多”并添加一些无法进入其中任何一个的更多验证,我正在寻找另一个尝试捕获。
我试图去“CoreValidation”类,提取出switch语句和属性,并把它放在一个新的类中。
然后我的计划是使用 swtich 语句调用该类和方法。所以在“更多”验证类中我会在这个
public string this[string columnName]
{
get
{
CoreValidtion(columnName); // this would contain the switch statement
//add another switch here with extra validation.
}
}
return "";
然后我将只有一次尝试捕获并且我不会复制任何代码,因为所有内容都会调用相同的方法(CoreValidation)。
但 updateModel 似乎无法解决这个问题。当 UpdateModel 尝试设置它需要设置的任何内容时,它找不到属性方法,因此所有属性都像
“用户名”都设置为空。
那我该怎么做我想做的事呢?
谢谢
【问题讨论】:
标签: asp.net-mvc validation idataerrorinfo