无法在 asp.net mvc System.Data.Entity.Validation.DbEntityValidationException 中的数据库优先方法中应用验证:
Not able to apply validation in the database first approach in asp.net mvc System.Data.Entity.Validation.DbEntityValidationException:
我制作了一个小型 mvc 应用程序,并没有尝试对其实施验证。为此,我尝试使用本教程
http://www.tutorialsteacher.com/mvc/implement-validation-in-asp.net-mvc
我做了完全正确的事情,但出现错误
我的代码如下
型号Class
namespace Bittu2
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class student1
{
public int StudentId { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
public string Branch { get; set; }
[Display(Name = "Mobile Number:")]
[Required(ErrorMessage = "Mobile Number is required.")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Mobile Number.")]
public Nullable<int> Mobile { get; set; }
}
}
create
Home Controller
中的操作方法
private StudentDemoEntities studentDemoEntities = new StudentDemoEntities();
public ActionResult Index()
{
var s = from student1 in studentDemoEntities.student1 select student1;
return View(s);
}
[HttpPost]
public ActionResult Create(String Name, String Branch, int Mobile)
{
student1 stud = new student1();
stud.Name = Name;
stud.Branch = Branch;
stud.Mobile = Mobile;
if(ModelState.IsValid)
{
studentDemoEntities.student1.Add(stud);
studentDemoEntities.SaveChanges(); //getting exception here. System.Data.Entity.Validation.DbEntityValidationException: 'Validation failed for one or more entities.
return RedirectToAction("Index");
}
return View();
}
我的 create
观点
model Bittu2.student1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
@using (Html.BeginForm("Create", "Home"))
{
<table>
<tr>
<td>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
</td></tr>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Branch)
</td>
<td>
@Html.TextBoxFor(model => model.Branch)
@Html.ValidationMessageFor(model => model.Branch, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
</td>
<td>
@Html.TextBoxFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
</td>
</tr>
</table>
<input type="submit" value="Create" />
}
<p>
</p>
@Html.ActionLink("Back to List", "Index")
</body>
</html>
如果输入错误,我应该会在视图本身上收到消息,但我不明白哪里出了问题。
您应该将模型传递给创建操作,而不是传递多个字符串。将 student1
class 传递给 Create
操作,然后调用 ModelState.IsValid
检查模型是否有效。
public ActionResult Create(student1 student)
{
if(ModelState.IsValid)
{
studentDemoEntities.student1.Add(student);
studentDemoEntities.SaveChanges()
return RedirectToAction("Index");
}
return View(student);
}
您还可以在调用 SaveChanges
时检查哪个 属性 无效:
if(ModelState.IsValid)
{
studentDemoEntities.student1.Add(stud);
try
{
return studentDemoEntities.SaveChanges();
return RedirectToAction("Index");
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
return Index(student);
}
}
我制作了一个小型 mvc 应用程序,并没有尝试对其实施验证。为此,我尝试使用本教程
http://www.tutorialsteacher.com/mvc/implement-validation-in-asp.net-mvc
我做了完全正确的事情,但出现错误
我的代码如下
型号Class
namespace Bittu2
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class student1
{
public int StudentId { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
public string Branch { get; set; }
[Display(Name = "Mobile Number:")]
[Required(ErrorMessage = "Mobile Number is required.")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Mobile Number.")]
public Nullable<int> Mobile { get; set; }
}
}
create
Home Controller
private StudentDemoEntities studentDemoEntities = new StudentDemoEntities();
public ActionResult Index()
{
var s = from student1 in studentDemoEntities.student1 select student1;
return View(s);
}
[HttpPost]
public ActionResult Create(String Name, String Branch, int Mobile)
{
student1 stud = new student1();
stud.Name = Name;
stud.Branch = Branch;
stud.Mobile = Mobile;
if(ModelState.IsValid)
{
studentDemoEntities.student1.Add(stud);
studentDemoEntities.SaveChanges(); //getting exception here. System.Data.Entity.Validation.DbEntityValidationException: 'Validation failed for one or more entities.
return RedirectToAction("Index");
}
return View();
}
我的 create
观点
model Bittu2.student1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
@using (Html.BeginForm("Create", "Home"))
{
<table>
<tr>
<td>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
</td></tr>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Branch)
</td>
<td>
@Html.TextBoxFor(model => model.Branch)
@Html.ValidationMessageFor(model => model.Branch, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
</td>
<td>
@Html.TextBoxFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
</td>
</tr>
</table>
<input type="submit" value="Create" />
}
<p>
</p>
@Html.ActionLink("Back to List", "Index")
</body>
</html>
如果输入错误,我应该会在视图本身上收到消息,但我不明白哪里出了问题。
您应该将模型传递给创建操作,而不是传递多个字符串。将 student1
class 传递给 Create
操作,然后调用 ModelState.IsValid
检查模型是否有效。
public ActionResult Create(student1 student)
{
if(ModelState.IsValid)
{
studentDemoEntities.student1.Add(student);
studentDemoEntities.SaveChanges()
return RedirectToAction("Index");
}
return View(student);
}
您还可以在调用 SaveChanges
时检查哪个 属性 无效:
if(ModelState.IsValid)
{
studentDemoEntities.student1.Add(stud);
try
{
return studentDemoEntities.SaveChanges();
return RedirectToAction("Index");
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
return Index(student);
}
}