我正在寻找如何在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中获取增加数据
发表回复