2013-03-10 84 views
1

我正在使用免费的Kendo web控件。我之前在几个地方使用了网格视图,并决定使用弹出式样式编辑来处理当前项目。Kendo.Web Grid Popup使用组合框创建

我大部分工作。我有三个组合框用于分类,银行账户和收款人,当我编辑现有项目时,传回我的MVC操作的模型对象具有正确的值。但是,当我单击创建按钮时,三个组合框值将作为null返回给控制器。

下面是对这一观点的CSHTML代码:

@using System 
@using System.Linq 

@{ 
    ViewBag.Title = "Transactions"; 
} 

@section Head 
{ 
    <link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" /> 
    <link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" /> 
    <script src="~/Scripts/kendo/kendo.web.min.js"> </script> 
} 

@section featured 
{ 
    <section class="featured"> 
     <div class="content-wrapper"> 
      <hgroup class="title"> 
       <h1>@ViewBag.Title</h1> 
      </hgroup> 
     </div> 
    </section> 
} 

<div id="grid"></div> 

<script> 
    $(function() { 
     $("#grid").kendoGrid({ 
      height: 350, 
      toolbar: [{ name: "create", text: "Create New Transaction" }], 
      columns: 
      [ 
       { field: "Date", width: "100px", template: '#= kendo.toString(Date,"MM/dd/yyyy") #' }, 
       { field: "Amount", format: "{0:c}", width: "100px" }, 
       { field: "Category", width: "80px", editor: categoryDropDownEditor, template: "#=Category.Name#" }, 
       { field: "BankAccount", title: "Account", width: "80px", editor: bankAccountDropDownEditor, template: "#=BankAccount.Name#" }, 
       { field: "Payee", width: "80px", editor: payeeDropDownEditor, template: "#=Payee.Name#" }, 
       { command: ["edit", "destroy"], title: "&nbsp;", width: "160px" } 
      ], 
      editable: { mode: "popup", confirmation: "Are you sure you want to delete this transaction?" }, 
      pageable: 
      { 
       refresh: true, 
       pageSizes: true 
      }, 
      sortable: true, 
      filterable: false, 
      dataSource: 
      { 
       serverPaging: true, 
       serverFiltering: true, 
       serverSorting: true, 
       pageSize: 7, 
       schema: 
       { 
        data: "Data", 
        total: "Total", 
        model: 
        { 
         id: "Id", 
         fields: 
         { 
          Id: { editable: false, nullable: true }, 
          Date: { type: "Date" }, 
          Amount: { type: "number", validation: { required: true, min: 0 } }, 
          Category: { validation: { required: true } }, 
          BankAccount: { validation: { required: true } }, 
          Payee: { validation: { required: true } }, 
          Note: { validation: { required: false } } 
         } 
        } 
       }, 
       batch: false, 
       transport: 
       { 
        create: 
        { 
         url: "@Url.Action("Create", "Transaction")", 
         contentType: "application/json", 
         type: "POST" 
        }, 
        read: 
        { 
         url: "@Url.Action("Read", "Transaction")", 
         contentType: "application/json", 
         type: "POST" 
        }, 
        update: 
        { 
         url: "@Url.Action("Update", "Transaction")", 
         contentType: "application/json", 
         type: "POST" 
        }, 
        destroy: 
        { 
         url: "@Url.Action("Delete", "Transaction")", 
         contentType: "application/json", 
         type: "POST" 
        }, 
        parameterMap: function(data) 
        { 
         return JSON.stringify(data); 
        } 
       } 
      } 
     }); 

     function categoryDropDownEditor(container, options) 
     { 
      $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:' + options.field + '"/>') 
       .appendTo(container) 
       .kendoDropDownList(
        { 
         autoBind: true, 
         dataValueFileld: "Id", 
         dataTextField: "Name", 
         dataSource: 
         { 
          type: "json", 
          transport: { read: "@Url.Action("GetCategories", "Transaction")" } 
         } 
        }); 
     } 

     function bankAccountDropDownEditor(container, options) 
     { 
      $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:' + options.field + '"/>') 
       .appendTo(container) 
       .kendoDropDownList(
        { 
         autoBind: true, 
         dataValueFileld: "Id", 
         dataTextField: "Name", 
         dataSource: 
         { 
          type: "json", 
          transport: { read: "@Url.Action("GetBankAccounts", "Transaction")" } 
         } 
        }); 
     } 

     function payeeDropDownEditor(container, options) 
     { 
      $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:' + options.field + '"/>') 
       .appendTo(container) 
       .kendoDropDownList(
        { 
         autoBind: true, 
         dataValueFileld: "Id", 
         dataTextField: "Name", 
         dataSource: 
         { 
          type: "json", 
          transport: { read: "@Url.Action("GetPayees", "Transaction")" } 
         } 
        }); 
     } 
    }); 
</script> 

结合到剑道组合框必须正常工作,否则编辑也会失败。我能想到的是该对象没有正确创建。此外,它默认选择组合框中的第一项,但即便如此,也不会绑定该值。

以下是我的创造和更新操作的代码:

[HttpPost] 
    public ActionResult Create(TransactionModel transactionModel) 
    { 
     var transaction = _moneyBO.CreateTransaction(); 

     Mapper.Map(transactionModel, transaction); 

     _moneyBO.UpdateTransaction(transaction); 

     return Json(Mapper.Map<TransactionModel>(transaction)); 
    } 

    public ActionResult Update(TransactionModel transactionModel) 
    { 
     var transaction = _moneyBO.Transactions.SingleOrDefault(x => x.Id == transactionModel.Id); 

     if (transaction == null) 
      return View("NotFound"); 

     Mapper.Map(transactionModel, transaction); 

     _moneyBO.UpdateTransaction(transaction); 

     return Json(Mapper.Map<TransactionModel>(transaction)); 
    } 

我还没有发现使用弹出自定义编辑一个很好的例子。 Kendo网站上的示例以内联方式工作,但如果将示例更改为弹出式窗口,则不起作用。

回答

0

不确定这是否是唯一的问题,但在您的代码示例中,它看起来像您的下拉菜单的初始化不完全正确。你已经写dataValueFileld这应该是dataValueField

kendoDropDownList({ 
    autoBind: true, 
    dataValueFileld: "Id",   <-- Incorrect spelling 
    dataTextField: "Name", 
    dataSource: 
    { 
     type: "json", 
     transport: { read: "@Url.Action("GetPayees", "Transaction")" } 
    } 
}); 
+0

我没有注意到。谢谢,我今晚会尝试一下,看看有没有什么不同。我的猜测可能不是,因为编辑工作在相同的连接,但谁知道。 – 2013-03-13 18:27:16

+0

上周我一直忙于其他项目。希望在这个周末看看这个。 – 2013-03-23 02:00:43

1

我有同样的问题。写,如果你解决它,请 我发现,Kendo认为“null”(默认为int?)是ObservableObject(当ComboBox初始化时),这就是为什么它不能被解析为“number”。如果你编辑项目(不创建),价值id不是“null”和模型bindind工作很好