-2
我遇到了無法將記錄保存到數據庫中的問題。我在一個視圖中有3個模型。我做了這個在一個視圖中通過多個模型創建新記錄
型「System.Data.Entity.Validation.DbEntityValidationException」發生在EntityFramework.dll的例外視圖,並得到錯誤,但在用戶代碼
其他信息沒有處理:一個或多個實體的驗證失敗。 有關更多詳細信息,請參閱「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);
}
你爲什麼要叫'db.PurchaseInvoiceDetailsTables.Add(model.PurchaseInvoiceDetailsTable);'和'db.PaymentTerms。添加(model.PaymentTerm);'兩次。什麼_See'EntityValidationErrors'屬性的更多細節_告訴你 –
爲什麼在世界上你有所有這些隱藏的輸入。使用視圖模型。 –
兩次是我做測試沒有刪除==,隱藏的輸入是當我創建它時會自動插入不會讓用戶鍵入隱藏的值 – nonstop328