2016-11-29 14 views
-2

レコードをデータベースに保存できないという問題が発生しました。私は1つのビューで3つのモデルを持っています。私はこの複数のモデルで1つのビューで新しいレコードを作成

EntityFramework.dllで発生した型「System.Data.Entity.Validation.DbEntityValidationException」の例外に関する観点となってエラーを行っているが、ユーザーコード

追加情報では処理されませんでした。 1つまたは複数のエンティティの検証に失敗しました。 詳細については、 '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

は、なぜあなたはdb.PurchaseInvoiceDetailsTables.Add(model.PurchaseInvoiceDetailsTable) '呼び出している:あなたはこれを使用することができます検証エラーを見るために

。 (model.PaymentTerm);を2回追加する。さらに、_ EntityValidationErrorsプロパティを参照してください詳細はこちら –

+1

そして、なぜ世界中に隠れた入力があるのですか?ビューモデルを使用する。 –

+0

2回はテストを行っていません==、隠れた入力は自動的に挿入されますが、ユーザは隠し値を入力しません。 – nonstop328

答えて

0

であるあなたが取得している例外は、あなたが保存しようとしている事業体の特性の1つ以上ではないことを意味し有効です。例えば列がnullではなく、保存しようとしているプロパティがnullであると定義されています。 ; `と` db.PaymentTerms

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); 
     } 
    } 
} 

credits

関連する問題