Asp.net mvc 使用ComboBox创建Web网格弹出窗口

Asp.net mvc 使用ComboBox创建Web网格弹出窗口,asp.net-mvc,kendo-grid,Asp.net Mvc,Kendo Grid,我正在使用免费的剑道网页控件。我以前在几个地方使用过网格视图,并决定在当前项目中使用弹出式编辑 我大部分时间都在工作。我有三个组合框用于分类、银行账户和收款人,当我编辑现有项目时,传递回MVC操作的模型对象中包含正确的值。但是,当我单击create按钮时,三个组合框值将作为null返回给控制器 以下是此视图的CSHTML代码: @using System @using System.Linq @{ ViewBag.Title = "Transactions"; } @section

我正在使用免费的剑道网页控件。我以前在几个地方使用过网格视图,并决定在当前项目中使用弹出式编辑

我大部分时间都在工作。我有三个组合框用于分类、银行账户和收款人,当我编辑现有项目时,传递回MVC操作的模型对象中包含正确的值。但是,当我单击create按钮时,三个组合框值将作为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>
@使用系统
@使用System.Linq
@{
ViewBag.Title=“交易”;
}
@科长
{
}
@特色栏目
{
@视图包。标题
}
$(函数(){
$(“#网格”).kendoGrid({
身高:350,
工具栏:[{名称:“创建”,文本:“创建新事务”},
柱:
[
{字段:“日期”,宽度:“100px”,模板:'#=kendo.toString(日期,“MM/dd/yyyy”)#',
{字段:“数量”,格式:{0:c},宽度:{100px},
{字段:“Category”,宽度:“80px”,编辑器:categorhydropdowneditor,模板:“#=Category.Name#”},
{字段:“BankAccount”,标题:“Account”,宽度:“80px”,编辑器:bankAccountDropDownEditor,模板:“#=BankAccount.Name#”},
{字段:“收款人”,宽度:“80px”,编辑器:PayedRopDownEditor,模板:“#=收款人.名称#”},
{命令:[“编辑”、“销毁”],标题:,宽度:“160px”}
],
可编辑:{模式:“弹出”,确认:“是否确实要删除此事务?”},
可分页:
{
刷新:是的,
页面大小:正确
},
可排序:是的,
可过滤:false,
数据源:
{
对,,
是的,
对,,
页面大小:7,
模式:
{
数据:“数据”,
总计:“总计”,
型号:
{
id:“id”,
领域:
{
Id:{可编辑:false,可空:true},
日期:{type:“Date”},
金额:{type:“number”,验证:{required:true,min:0},
类别:{验证:{必需:true}},
银行帐户:{验证:{必需:true}},
收款人:{验证:{必需:true}},
注意:{验证:{必需:false}}
}
}
},
批次:假,
运输:
{
创建:
{
url:“@url.Action(“创建”、“事务”)”,
contentType:“应用程序/json”,
类型:“职位”
},
阅读:
{
url:“@url.Action”(“读取”,“事务”)”,
contentType:“应用程序/json”,
类型:“职位”
},
更新:
{
url:“@url.Action”(“更新”,“事务”)”,
contentType:“应用程序/json”,
类型:“职位”
},
销毁:
{
url:“@url.Action”(“删除”,“事务”)”,
contentType:“应用程序/json”,
类型:“职位”
},
parameterMap:函数(数据)
{
返回JSON.stringify(数据);
}
}
}
});
函数类别HydropDownEditor(容器、选项)
{
$('')
.appendTo(容器)
.kendoDropDownList(
{
自动绑定:是的,
dataValueFileld:“Id”,
dataTextField:“名称”,
数据源:
{
键入:“json”,
传输:{读取:@Url.Action(“GetCategories”,“Transaction”)”}
}
});
}
函数bankAccountDropDownEditor(容器,选项)
{
$('')
.appendTo(容器)
.kendoDropDownList(
{
自动绑定:是的,
dataValueFileld:“Id”,
dataTextField:“名称”,
数据源:
{
键入:“json”,
传输:{读取:“@Url.Action”(“GetBankAccounts”,“Transaction”)”}
}
});
}
函数PayedRopDownEditor(容器、选项)
{
$('')
.appendTo(容器)
.kendoDropDownList(
{
自动绑定:是的,
dataValueFileld:“Id”,
dataTextField:“名称”,
数据源:
{
键入:“json”,
传输:{读取:@Url.Action(“GetPayes”,“Transaction”)}
}
});
}
});
剑道组合框的绑定必须正常工作,否则编辑也会失败。我所能想到的就是对象没有正确创建
    [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));
    }
kendoDropDownList({
    autoBind: true,
    dataValueFileld: "Id",          <-- Incorrect spelling
    dataTextField: "Name",
    dataSource:
    {
        type: "json",
        transport: { read: "@Url.Action("GetPayees", "Transaction")" }
    }
});