【发布时间】:2012-07-04 10:13:45
【问题描述】:
假设我有 2 个模型:
客户(ID、姓名) 发票(ID、姓名、价值)。
现在,Route /Customer/Create 允许我在 TelerikGrid 下方创建一个新客户(名称的一个文本框),它会向客户添加发票。
我的解决方案是:
- 创建客户(使用默认名称,例如“-”)
- 保存到数据库
- 读取 CustomerId
- 将发票添加到此 CustomerId
- 再次拯救客户
我的目标:
/Customer/Create/ -> 创建一个新的CustomerViewModel-Object,用网格显示视图,它绑定到InvoiceViewModel(CustomerViewModel的属性)
.DataBinding(dataBinding => dataBinding
.Ajax()
.Insert("InsertInvoice", "Customer")
ajax-Method 应该是这样的:
private CustomerViewModel myCustomerViewModel;
public ActionResult Create()
{
this.myCustomerViewModel = new CustomerViewModel();
this.myCustomerViewModel = new List<InvoiceViewModel>();
return View(this.myCustomerViewModel);
}
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult InsertInvoice(InvoiceViewModel newInvoice)
{
this.myCustomerViewModel.Invoices.Add(newInvoice)
return View(new GridModel(this.myCustomerViewModel.Invoices));
}
最后,当我单击“创建”按钮创建新客户时,它应该将其保存到数据库。
现在我卡住了,因为当我调用“InsertInvoice”-Method 时,myCustomerViewModel 为 NULL。
想法?
最好的问候, 戴夫
【问题讨论】: