2016-11-29 1 views
-2

Ich habe ein Problem konfrontiert, das den Datensatz nicht in der Datenbank speichern kann. Ich habe 3 Modelle in einer Ansicht. Ich habe die Ansicht und immer Fehler zu dieser in EntityFramework.dll aufgetreten neuen Datensatz über mehrere Modelle in einer Ansicht erstellen

Eine Ausnahme vom Typ ‚System.Data.Entity.Validation.DbEntityValidationException‘

getan wurde aber in Benutzercode

Zusätzliche Informationen nicht behandelt: Die Validierung ist für eine oder mehrere Entitäten fehlgeschlagen. Weitere Informationen finden Sie unter Eigenschaft 'EntityValidationErrors'.

hier ist meine Ansicht

<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> 

hier ist mein Controller

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

Warum rufen Sie 'db.PurchaseInvoiceDetailsTables.Add (model.PurchaseInvoiceDetailsTable);' und 'db.PaymentTerms. Add (model.PaymentTerm); zweimal. Und was sagt _See 'Eigenschaft' EntityValidationErrors 'für mehr Details_ Ihnen –

+1

Und warum in der Welt haben Sie alle diese versteckten Eingaben. Verwenden Sie Ansichtsmodelle. –

+0

zweimal mache ich das Testen löschte nicht ==, versteckte Eingabe ist für wenn ich es erstelle wird automatisches Einfügen wird nicht lassen Benutzer geben Sie den versteckten Wert ein – nonstop328

Antwort

0

Die Ausnahme, die Sie Mittel bekommen, dass eine oder mehrere der Einheiten Eigenschaften, die Sie nicht versuchen, zu speichern ist gültig. z.B. Die Spalte ist nicht null und die Eigenschaft, die Sie speichern möchten, ist null.

Um die Validierungsfehler sehen Sie diese verwenden können:

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

Verwandte Themen