2016-12-07 56 views
0

如何通过javascript从动态添加文本框中保存多个数据?我已经完成了产品列表和付款条款的动态添加文本框。我应该怎么控制器和视图做如何在mvc动态添加文本框中保存数据库中的多个数据

我有测试This但没有奏效

这里是我的控制器

public ActionResult Create([Bind(Include = "PurchaseInvoiceTable,PurchaseInvoiceDetailsTable,PaymentTerm")]InvoiceWrapper model)    
{ 
    try 
    { 

     if (ModelState.IsValid) 
     { 

      db.PurchaseInvoiceTables.Add(model.PurchaseInvoiceTable);      
      db.SaveChanges(); 

      var PID = model.PurchaseInvoiceTable.PurchaseInvoiceID; 
      model.PurchaseInvoiceDetailsTable.InvoiceID = PID; 
      model.PaymentTerm.InvoiceID = PID; 

      db.PurchaseInvoiceDetailsTables.Add(model.PurchaseInvoiceDetailsTable); 
      db.PaymentTerms.Add(model.PaymentTerm); 
      db.SaveChanges(); 

      return RedirectToAction("Index"); 
     } 

     ViewBag.InvoiceID = new SelectList(db.PurchaseInvoiceTables, "PurchaseInvoiceID", "PurID", model.PurchaseInvoiceDetailsTable.InvoiceID); 
     ViewBag.InvoiceID = new SelectList(db.PurchaseInvoiceTables, "PurchaseInvoiceID", "PurID", model.PaymentTerm.InvoiceID);    
     return View(model); 
    } 
    catch (DbEntityValidationException e) 
    { 
     //catch validation here 
    } 

} 

这里是我的invoiceWrapper

using System; 
using System.Collections.Generic; 

namespace OnlineInvoiceSystem.Models 
{ 
    public class InvoiceWrapper 
    { 
     public PurchaseInvoiceTable PurchaseInvoiceTable { get; set; } 
     public PurchaseInvoiceDetailsTable PurchaseInvoiceDetailsTable { get; set; } 
     public PaymentTerm PaymentTerm { get; set; } 
    } 
} 

这里我的javascript You can test it on Jsfiddle

我的v IEW是这个

@model OnlineInvoiceSystem.Models.InvoiceWrapper 

@{ Layout = null; Layout = "~/Views/Shared/_Layout.cshtml"; } 

@using (Html.BeginForm()) { 

@Html.AntiForgeryToken() 

<form> 

    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

    <h2>Product List</h2> 

    <div class="panel panel-default product_wrapper"> 

    //dynamic added textbox will appear here 

    </div> 



    <button class="add_field_button btn btn-primary pull-right">Add More Fields</button> 

    <div class="wrapper-payment-details"> 

    <tr style="background-color:black; color:white;" class="payment_term_wrapper"> 

     // here is the dynamic added payment terms textbox 

    </tr> 

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

</form> 
    } 
+1

参考[this answer](http://stackoverflow.com/questions/ 28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)一些选项 –

+0

@StephenMuecke [The Begin Collection适用于所有MVC版本? ](https://github.com/danludwig/BeginCollectionItem)目前我正在使用MVC 5 – nonstop328

+0

是的,它适用于MVC-5 –

回答

-1

你应该使用JavaScript AJAX(POST调用),包含所有动态添加字段的JSON(如可能的项值对象的列表),并在服务器端,您需要一个动作方法可以处理列表,你不能直接从MVC做它。

+2

当然,你可以从MVC –

+0

发布一个解决方案,然后 –

+0

我给了一个链接对该问题的评论:) –

0

你应该建模 @ Html.TextBoxFor(M => m.PurchaseInvoiceID,新{ID = “PurchaseInvoiceID”,@类= “形式控制”})
@ Html.TextBoxFor(M =>米。 InvoiceID,new {id =“InvoiceID”,@ class =“form-control”})
然后通过这个变量通过ajax或发布json

+0

可以更具体吗?我不明白 – nonstop328

+0

For refrence这是我的模特类 –

+0

公共部分班学生 { public int Id {get;组; } public string Name {get;组; } public Nullable Standard {get;组; } public Nullable Marks {get;组; } public Nullable Dob {get;组; } public int Age {get;组; } } –

相关问题