动态数据| LINQ TO SQL |一般验证
我有 5 个不同的实体,为其生成了动态数据(使用 LINQTOSQL)。 在任何这些实体的插入(Insert.aspx)上,如果出现错误,我想通知用户发生了错误,并可能显示一些通用错误消息。
1)我不是在谈论常规的必填字段错误,而是像“违反唯一约束”之类的内容
2)我可以通过执行以下操作分别为每个页面执行此操作:
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
if (e.Exception == null || e.ExceptionHandled)
{
Response.Redirect(table.ListActionPath);
}
else
{
//OtherErrors is the label on the page
OtherErrors.Visible = true;
OtherErrors.Text = e.Exception.Message;
OtherErrors.DataBind();
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
}
}
3)但是,我想创建一些非常通用的内容将适用于所有实体的所有插入
I have 5 different entities for which dynamic data(with LINQTOSQL) was generated.
On Insert(Insert.aspx) of any of these entities, if there is an error, I would like to notify user that error happened and possibly show some generic error message.
1) I am not talking about regular required field errors but something like "Unique constraint violation"
2) I can do it for each page separately by doing something like this:
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
if (e.Exception == null || e.ExceptionHandled)
{
Response.Redirect(table.ListActionPath);
}
else
{
//OtherErrors is the label on the page
OtherErrors.Visible = true;
OtherErrors.Text = e.Exception.Message;
OtherErrors.DataBind();
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
}
}
3) BUT, I want to created something very generic that will work for all inserts across all entities
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过在 ADO.NET 实体框架类中创建事件处理程序来自定义验证:
DynamicValidator 控件将捕获数据模型中引发的任何验证异常。动态数据项目中包含的页面模板包含 DynamicValidator 控件,该控件在页面上显示验证错误。
You can customize validation by creating an event handler in the ADO.NET Entity Framework class:
Any validation exceptions that are thrown in the data model are caught by the DynamicValidator control. The page templates included with a Dynamic Data project contain a DynamicValidator control, which displays the validations errors on the page.
我无法针对您的情况完全测试这一点,但是您可以重写
SubmitChanges
方法。I can't fully test this for your situation but, you could override the
SubmitChanges
method.