关于程序员:如何在ASPNET-MVC-5中动态添加新行

5次阅读

共计 1435 个字符,预计需要花费 4 分钟才能阅读完成。

我正在寻找如何在 ASP.NET MVC 5 应用程序的 Create Razor 视图中为 Invoice 类增加新行的 LineItem. 我曾经浏览了简直所有相似的问题,但没有人解决了我认为是一个简略的用例.

这是我的发票模型类

public class Invoice
    {public int Id { get; set;}
        public int InvoiceNumber {get; set;}
        public List<LineItem> LineItems {get; set;}
        public Client Customer {get; set;}
        public DateTime DateCreated {get; set;}        
        public decimal Total {get; set;}            


        public Invoice()
        {LineItems = new List<LineItem>();
        }

留神,此发票蕴含 LineItems 列表,每个行项都是一个简略的对象. 并在发票构造函数中创立行项列表.
这是 LineItem 类

public class LineItem
    {public int Id { get; set;}
        public string Name {get; set;}
        public string Description {get; set;}
        public int Quantity {get; set;}
        public decimal Price {get; set;}        
        public decimal Total {get; set;}

    }

生成的 asp.net mvc 5 razor 视图未辨认对象的 LineItems 列表,并且没有为其创立任何条目. 我想动静地向下表增加一行,我想制作该行的行我的项目实例.
以下是发票前端显示表格

<table class="table table-condensed" id="invoiceTable"> 
      <thead>
         <tr id="invoiceTableHead">
             <td><strong>Item Name</strong></td>
             <td class="text-center"><strong>Item Description</strong></td>
             <td class="text-center"><strong>Item Price</strong></td>
             <td class="text-center"><strong>Item Quantity</strong></td>
             <td class="text-right"><strong>Total</strong></td>
          </tr>
       </thead>

     <tbody>

以下是我尝试应用 jquery 动静地将行追加到此表,但无奈达到想要的成果。

    <script type="text/javascript">
    $("#lineItemButton").click(function () {
        debugger;
        // Create elements dynamically
        var newRow = "<tr><td>'@Html.TextBoxFor(x => x.LineItems, new { ??? What do int public here)'</td></tr>";

        // Add the new dynamic row after the last row
            $('#invoiceTable tr:last').after(newRow);
        });

    </script>

解决方案参考:

1> 如何在 ASP.NET MVC 5 中动静增加新行

2> MVC3.0 动静增加表格的行数并 Controller 中获取增加数据

正文完
 0