我遇到了一个无法将记录保存到数据库中的问题。我在一个视图中有3个模型。我已完成视图并收到有关此
的错误EntityFramework.dll中出现'System.Data.Entity.Validation.DbEntityValidationException'类型的异常,但未在用户代码中处理
其他信息:一个或多个实体的验证失败。 有关详细信息,请参阅“EntityValidationErrors”属性。
这是我的观点
<div class="container">
<div class="row">
<div class="col-lg-12 management-title">
<h1>Create New Invoice</h1>
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<form>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.CompanyID, "CompanyID",new { @Name="CompanyID"})
@Html.DropDownList("CompanyID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PurchaseInvoiceTable.CompanyID , "", new { @class = "text-danger" })
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#bill_to_company">Company Details</a>
</h4>
</div>
<div id="bill_to_company" class="panel-collapse collapse in wrapper-company-details">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.CompanyName)
@Html.TextBoxFor(model => model.PurchaseInvoiceTable.CompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.CompanyAddress)
@Html.TextAreaFor(model => model.PurchaseInvoiceTable.CompanyAddress, new { @class = "form-control golbal_textarea" })
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.startdate)
@Html.TextBoxFor(model => model.PurchaseInvoiceTable.startdate, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
</div>
<div class="form-group">
</div>
</div>
</div>
</div>
</div>
<h2>Product List</h2>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#product_list">Product List</a>
</h4>
</div>
<div id="product_list" class="panel-collapse collapse in input_fields_wrap">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel panel-default product_wrapper">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#product' + x + '">Product</a>
</h4>
</div>
<div id="product'+x+'" class="panel-collapse collapse in">
<div class="panel-group">
<div class="panel panel-default">
<div class="col-lg-12">
<div class="col-lg-3">
<label>Product</label>
@Html.TextAreaFor(model => model.PurchaseInvoiceDetailsTable.Product)
</div>
<div class="col-lg-6">
<label>Description</label>
@Html.TextAreaFor(model => model.PurchaseInvoiceDetailsTable.ProductDescription)
</div>
'<div class="col-lg-2 form-group">
<label>Price</label>
@Html.TextBoxFor(model => model.PurchaseInvoiceDetailsTable.Price)
</div>
</div>
</div>
</div>
</div>
<a href="#" class="remove_field btn btn-danger pull-right">cancel</a>
</div>
</div>
</div>
</div>
</div>
</div>
<button class="add_field_button btn btn-primary pull-right">Add More Fields</button>
<div class="wrapper-company-details">
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.Remark)
@Html.TextAreaFor(model => model.PurchaseInvoiceTable.Remark, new { @class = "form-control", @style = "resize:none;" })
@Html.ValidationMessageFor(model => model.PurchaseInvoiceTable.Remark, "", new { @class = "text-danger" })
</div>
<h1>Payment Term Area</h1>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#payment_term_collapse">Payment Term</a>
</h4>
</div>
<div id="payment_term_collapse" class="payment_term_area panel-collapse collapse in">
<table class="table table-bordered">
<tr style="background-color:black; color:white;">
<th style="width:70%;">
<input type="text" class="form-control" id="payment_term" placeholder="Type how many terms client want" />
</th>
<th style="width:30%;">
<button class="btn btn-default" id="add_payment_terms">Add</button>
</th>
</tr>
</table>
<table class="table table-bordered">
<tr style="background-color:black; color:white;">
<th style="width:70%;">
Payment Term
</th>
<th style="width:30%;">
Amount
</th>
</tr>
<tr style="background-color:black; color:white;">
<th style="text-align:right;">
Discount
</th>
<th>
@Html.TextBoxFor(model => model.PurchaseInvoiceTable.Discount, new { @class = "form-control discount price_textbox", @id = "discount", @Value = "0" })
</th>
</tr>
<tr style="background-color:black; color:white;">
<th style="text-align:right;">
Total
</th>
<th>
@Html.TextBoxFor(model => model.PurchaseInvoiceTable.Subtotal, new { @class = "form-control price_textbox total", @id = "totval", @Value = "0" })
</th>
</tr>
</table>
<div id="payment_term_area">
<h2>Payment Terms</h2>
<div class="payment_term_wrapper" style="padding:0; margin:0;">
<table class="table table-bordered" style="width:95%;">
<tr style="background-color:black; color:white;" class="payment_term_wrapper">
<th style="width:70%;">
<input class="pull-right payment_terms_label_input" style="background-color:black; color:white;"></input>
</th>
<th style="width:30%;">
@Html.TextBoxFor(model => model.PaymentTerm.Amount)
</th>
</tr>
<a href="#" class="remove_field btn btn-danger pull-right">X</a>
</table>
</div>
</div>
<table class="table table-bordered">
<tr style="background-color:black; color:white;">
<th style="text-align:right; width:70%;">
Last Payment
</th>
<th style="width:30%;">
@Html.TextBoxFor(model => model.PurchaseInvoiceTable.Total, new { @class = "form-control price_textbox", @id = "lastpayment", @Value = "0" })
</th>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="col-lg-12">
</div>
<div class="col-lg-12">
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.Category, "Category")
@Html.DropDownList("Category", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PurchaseInvoiceTable.Category, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.Method)
@Html.DropDownListFor(model => model.PurchaseInvoiceTable.Method, new List<SelectListItem>
{
new SelectListItem() {Text = "Cash", Value="Cash"},
new SelectListItem() {Text = "Cheque", Value="Cheque"},
new SelectListItem() {Text = "Bank Transfer", Value="BankTransfer"}
}, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PurchaseInvoiceTable.Method, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseInvoiceTable.PaymentTerm, "PaymentTerm", htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownListFor(model => model.PurchaseInvoiceTable.PaymentTerm, new List<SelectListItem>
{
new SelectListItem() {Text = "1", Value="1"},
new SelectListItem() {Text = "2", Value="2"},
new SelectListItem() {Text = "3", Value="3"}
}, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PurchaseInvoiceTable.PaymentTerm, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.PurchaseInvoiceTable.Status, new { @class = "form-control", @Value = "Active" })
@Html.HiddenFor(model => model.PurchaseInvoiceDetailsTable.Status, new { @class = "form-control", @Value = "Active" })
@Html.HiddenFor(model => model.PaymentTerm.Status, new { @class = "form-control", @Value = "Active" })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.PurchaseInvoiceTable.first_created, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
@Html.HiddenFor(model => model.PurchaseInvoiceDetailsTable.first_created, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
@Html.HiddenFor(model => model.PaymentTerm.first_created, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.PurchaseInvoiceTable.first_created_by, new { @class = "form-control", @Value = @Session["Username"].ToString() })
@Html.HiddenFor(model => model.PurchaseInvoiceDetailsTable.first_created_by, new { @class = "form-control", @Value = @Session["Username"].ToString() })
@Html.HiddenFor(model => model.PaymentTerm.first_created_by, new { @class = "form-control", @Value = @Session["Username"].ToString() })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.PurchaseInvoiceTable.last_updated, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
@Html.HiddenFor(model => model.PurchaseInvoiceDetailsTable.last_updated, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
@Html.HiddenFor(model => model.PaymentTerm.last_updated, new { @class = "form-control", @Value = @DateTime.Now.ToString() })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.PurchaseInvoiceTable.last_updated_by, new { @class = "form-control", @Value = @Session["Username"].ToString() })
@Html.HiddenFor(model => model.PurchaseInvoiceDetailsTable.last_updated_by, new { @class = "form-control", @Value = @Session["Username"].ToString() })
@Html.HiddenFor(model => model.PaymentTerm.last_updated_by, new { @class = "form-control", @Value = @Session["Username"].ToString() })
</div>
<div class="form-group">
<div>
<input type="submit" value="Create" class="btn btn-primary pull-right" />
@Html.ActionLink("Back", "Index", null, new { @class = "btn btn-small btn-danger pull-right", @style = "margin-right:2%;" })
</div>
</div>
</div>
</form>
}
</div>
</div>
这是我的控制器
public ActionResult Create()
{
ViewBag.CompanyID = new SelectList(db.SupplierTables, "SupplierID", "SupplierID");
ViewBag.Product = new SelectList(db.ProductTables, "ProductID", "ProductID");
ViewBag.Category = new SelectList(db.CategoryTables, "CategoryID", "CategoryID");
ViewBag.PaymentTerm = new SelectList(db.PaymentTerms, "TermID", "TermID");
return View();
}
[HttpPost]
public ActionResult Create([Bind(Include = "PurchaseInvoiceTable,PurchaseInvoiceDetailsTable,PaymentTerm")]InvoiceWrapper model)
{
if (ModelState.IsValid)
{
db.PurchaseInvoiceTables.Add(model.PurchaseInvoiceTable);
db.PurchaseInvoiceDetailsTables.Add(model.PurchaseInvoiceDetailsTable);
db.PaymentTerms.Add(model.PaymentTerm);
db.SaveChanges();
//model.PurchaseInvoiceTable.PurchaseInvoiceID = model.PurchaseInvoiceDetailsTable.InvoiceID = model.PaymentTerm.InvoiceID;
return RedirectToAction("Index");
}
ViewBag.CompanyID = new SelectList(db.SupplierTables, "SupplierID", "SupplierID");
ViewBag.Product = new SelectList(db.ProductTables, "ProductID", "ProductID");
ViewBag.Category = new SelectList(db.CategoryTables, "CategoryID", "CategoryID");
ViewBag.PaymentTerm = new SelectList(db.PaymentTerms, "TermID", "TermID");
return View(model);
}
答案 0 :(得分:0)
您获得的异常意味着您尝试保存的一个或多个实体属性无效。例如该列定义为非null,并且您尝试保存的属性为null。
为了查看验证错误,您可以使用:
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
}