我在打印验证方面遇到了问题 视图中的输入字段。
我有一个视图,我希望用户填写表单。在这种形式我 需要在用户继续之前验证输入。在我的dbModel中 我已经就如何验证代码设置了不同的规则。在 该视图我试图使用显示错误消息 Html.Validationsummary(真)
它在早期的项目中运作得非常好。但不是这个......
这是DbModel类
public class Kjop
{
[Key]
public int ordreid { get; set; }
public int kundeid { get; set; }
public int produktid { get; set; }
public DateTime tid { get; set; }
[Required(ErrorMessage = "Fornavn må oppgis")]
[StringLength(50, ErrorMessage = "Maks 50 bokstaver")]
[DisplayName("Fornavn")]
public string fornavn { get; set; }
[Required(ErrorMessage = "Etternavn må oppgis")]
[StringLength(50, ErrorMessage = "Maks 50 bokstaver")]
[DisplayName("Etternavn")]
public string etternavn { get; set; }
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$", ErrorMessage = "Ugylding formatering på epost")]
[Required(ErrorMessage = "feil i epost")]
[StringLength(50, ErrorMessage = "Maks 50 bokstaver")]
[DisplayName("Epost")]
public string epost { get; set; }
[Required(ErrorMessage = "Telefon må oppgis")]
[RegularExpression(@"[0-9]{8}", ErrorMessage = "8 tegn i telefonnr")]
[DisplayName("Telefon")]
public string telefon { get; set; }
[Required(ErrorMessage = "Adresse må oppgis")]
[StringLength(50, ErrorMessage = "Maks 50 bokstaver")]
[DisplayName("Adresse")]
public string adresse { get; set; }
[Required(ErrorMessage = "Postnr må oppgis")]
[RegularExpression(@"[0-9]{4}")]
[DisplayName("Postnummer")]
public string postnr { get; set; }
[Required(ErrorMessage = "Poststed må oppgis")]
[DisplayName("Poststed")]
public string poststed { get; set; }
public bool betalt { get; set; }
public int antall { get; set; }
}
变量名称是挪威语,没关系。代码仍然是 任何语言相同:)
我的控制器
public ActionResult Registrering()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Registrering(FormCollection innKunde)
{
try
{
using (var dbB = new databaseContext())
{
using (var dbP = new produktContext())
{
var produktId = from d in dbP.Produkter
where d.ProduktNavn.Equals(GlobalVariables.valgtProdukt)
select d;
int produktid = 0;
foreach (var b in produktId)
{
produktid = b.ProduktID;
}
var nyttKjop = new Kjop()
{
kundeid = 0,
produktid = produktid,
fornavn = innKunde["Fornavn"],
etternavn = innKunde["Etternavn"],
antall = Convert.ToInt32(GlobalVariables.produktAntall),
tid = DateTime.Now,
epost = innKunde["Epost"],
telefon = innKunde["Telefon"],
adresse = innKunde["Adresse"],
postnr = innKunde["Postnr"],
poststed = innKunde["Poststed"],
};
dbB.Kjoper.Add(nyttKjop);
dbB.SaveChanges();
Response.Write("Data lagt inn");
Session["Nykunde"] = nyttKjop;
//Adde sessions på variabler til kvitteringsiden
return RedirectToAction("Kredittkort");
}
}
}
catch (Exception feil)
{
Response.Write(feil);
return View();
}
}
和视图
@model Nettbutikk_v1.Models.Kjop
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Registrering</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Kjop</legend>
<div class="editor-label">
@Html.LabelFor(model => model.fornavn)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.fornavn)
@Html.ValidationMessageFor(model => model.fornavn)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.etternavn)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.etternavn)
@Html.ValidationMessageFor(model => model.etternavn)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.epost)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.epost)
@Html.ValidationMessageFor(model => model.epost)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.telefon)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.telefon)
@Html.ValidationMessageFor(model => model.telefon)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.adresse)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.adresse)
@Html.ValidationMessageFor(model => model.adresse)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.postnr)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.postnr)
@Html.ValidationMessageFor(model => model.postnr)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.poststed)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.poststed)
@Html.ValidationMessageFor(model => model.poststed)
</div>
<p>
<input type="submit" value="Registrer" />
</p>
</fieldset>
}
</body>
</html>
当我尝试解析某些内容时,我收到以下异常...
System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
有人可以帮我弄清楚我做错了吗?
答案 0 :(得分:0)
哟创建一个新的块try-catch DbEntityValidationException:
catch (DbEntityValidationException exDbEntityValidationException)
实施例
catch (DbEntityValidationException exDbEntityValidationException)
{
DbEntityValidationException exval = exDbEntityValidationException;
StringBuilder sb = new StringBuilder();
foreach (var excep in exval.EntityValidationErrors)
{
sb.AppendFormat("YourField: {0} {1}",((YourEntityType)excep.Entry.Entity).YourField, Environment.NewLine);
excep.ValidationErrors.ToList().ForEach(v => sb.AppendFormat(" Field: {0} - {1}{2}", v.PropertyName, v.ErrorMessage, Environment.NewLine));
}
System.Diagnostics.Debug.Print(sb.ToString());
}