我在这里试过了Append and Bind解决方案。 This solution
但仍然有问题。
我有两个型号。第一是定价
public class Pricing
{
public int PricingID { get; set; }
public int PricePerSack { get; set; }
public virtual ExpensePersack ExpensePerSacks { get; set; }
public string companyName { get; set; }
public string BankName { get; set; }
public string Branch { get; set; }
public Terms Term { get; set; }
public int ChequeNumber { get; set; }
public DateTime? DateofCheque { get; set; }
}
public enum Terms { Cash,Cheque};
}
接下来是ExpensePerSack模型。
public class ExpensePersack
{
public int ExpensePersackID { get; set; }
public int PricingID { get; set; }
public string Description { get; set; }
public int Amount { get; set; }
}
控制器
public ActionResult Create([Bind(Include = "PricingID,PricePerSack,companyName,BankName,Branch,Term,ChequeNumber,DateofCheque")] Pricing pricing, ExpensePersack ExpensePersack)
{
if (ModelState.IsValid)
{
db.Pricings.Add(pricing);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pricing);
}
现在在视图中。这是剪辑很难编辑这个问题因为复制粘贴时代码被破坏了。
现在在控制器中我还没有添加要保存的费用模型或其代码,因为在Post方法中。控制器的费用为空。
我仍然不知道如何通过ID发送数据到控制器甚至删除附加列。
@model RMQGrains.Models.Pricing
... bunch of codes from Pricing Models editor for...
<div id="InsertView"><div>
<input type="button" id="addrow" value="Add">
<div id="NewBatchProduct" style="display:none">
<tr>
<td><input type="text" name="ExpensePricing[#].Description" value /> </td>
<td><input type="text" name="ExpensePricing[#].Amount" value /></td>
<td>
<input type="hidden" name="ExpensePricing.Index" value="%" />
<a class="deleteRow">Delete</a>
</td>
</tr>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$("#addrow").click(function () {
var index = (new Date()).getTime(); // unique indexer
var clone = $('#NewBatchProduct').clone(); // clone the BatchProducts item
// Update the index of the clone
clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
$("#InsertView").append(clone.html());
});
</script>
}