【问题标题】:Telerik MVC Ajax Grid - Adding ViewModel to other ViewModel before save to DBTelerik MVC Ajax Grid - 在保存到数据库之前将 ViewModel 添加到其他 ViewModel
【发布时间】:2012-07-04 10:13:45
【问题描述】:

假设我有 2 个模型:

客户(ID、姓名) 发票(ID、姓名、价值)。

现在,Route /Customer/Create 允许我在 TelerikGrid 下方创建一个新客户(名称的一个文本框),它会向客户添加发票。

我的解决方案是:

  1. 创建客户(使用默认名称,例如“-”)
  2. 保存到数据库
  3. 读取 CustomerId
  4. 将发票添加到此 CustomerId
  5. 再次拯救客户

我的目标:

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

想法?

最好的问候, 戴夫

【问题讨论】:

    标签: telerik-grid telerik-mvc


    【解决方案1】:

    问题解决了,但我不知道它是否“干净”,尽管如此:

    旧:

    private CustomerViewModel myCustomerViewModel;
    

    现在:

    public CustomerViewModel CurrentCustomer
    {
        get
        {
            return (CustomerViewModel )Session["CurrentCustomer"];
        }
        set
        {
            Session["CurrentCustomer"] = value;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2017-12-26
      相关资源
      最近更新 更多