Kendo DateTimePicker从json结果中设置值

时间:2014-07-30 06:52:26

标签: json kendo-ui kendo-datetimepicker

我在json结果中粘贴Kendo Grid中的Kendo DateTimePicker中的值时遇到了大麻烦。所以我在页面上有两个表单。首先我加载文件:

@using (Html.BeginForm("GetImportSalesModelFromFile", "ExportImport", FormMethod.Post, new { id = "GetImportSaleModelFromFileForm", enctype = "multipart/form-data" }))
{
     <input id="importFileInput" type="file" accept="text/csv" name="file" class="user-success" required>
     <input style="width: 100px;" type="submit" value="Add">
}

在表单提交称为函数

$('#GetImportSaleModelFromFileForm').submit(function(e) {
    e.preventDefault();
    var url = $(this).attr('action');
    var xhr = new XMLHttpRequest();
    var fd = new FormData();
    fd.append("file", document.getElementById('importFileInput').files[0]);
    xhr.open("POST", url, true);
    xhr.send(fd);
    xhr.addEventListener("load", function(event) {
        AppendImportModel(JSON.parse(event.target.response));
    }, false);
});

在控制器中我需要导入模型

    public ActionResult GetImportSalesModelFromFile(HttpPostedFileBase file)
    {
        var importModel = _importService.ConstructSaleImportModel(file.InputStream, file.ContentType);
        return Json(importModel, JsonRequestBehavior.AllowGet);
    }

在函数AppendImportModel中我解析结果并将其粘贴到第二种形式的剑道网格中

@(Html.Kendo().Grid<ImportSaleItemModel>().Name("ImportSalesGrid")
                            .DataSource(dataSource => dataSource.Ajax())
                            .Events(x => x.DataBound("initMenus"))
                            .Columns(columns =>
                            {
                                columns.Bound(x => x.Goods.PictureId)
                                    .ClientTemplate("<img style='height: 50px;' src='" + Url.Action("Contents", "FileStorage", new { id = "#= Goods.PictureId #" }) + "'/>")
                                    .Title("")
                                    .Sortable(false)
                                    .HtmlAttributes(new Dictionary<string, object> { { "style", "padding: 3px !important; height: 52px !important; width:52px !important;" } });
                                columns.Bound(x => x.Goods.Title)
                                    .ClientTemplate("<a onclick='ShowInfoGoodWindow(#= Goods.Id #)'>#= Goods.Title #</a><br>" +
                                                    "<span><b>#= Goods.Article #</b> <descr>#= Goods.Description #</descr></span><br><input type='hidden' name='ImportedGoodList[#= index(data)#].Id' value='#= Goods.Id #' />")
                                    .Title("Description");
                                columns.Bound(x => x.Price)
                                    .ClientTemplate("<input class='priceEditor' maxlength='10' style='width:50px; text-align: center;' type='text' name='ImportedGoodList[#= index(data)#].Price' onkeypress='return isPriceKey(event)' oninput='$(this).get(0).setCustomValidity(clearValidation);' value='#=Price.ParsedValue #'>")
                                    .HtmlAttributes(new Dictionary<string, object> { { "style", "text-align: center;" } })
                                    .Title("Price");
                                columns.Bound(x => x.Discount)
                                    .ClientTemplate("<input class='discountEditor' maxlength='10' style='width:50px; text-align: center;' type='text' name='ImportedGoodList[#= index(data)#].Discount' onkeypress='return isPriceKey(event)' oninput='$(this).get(0).setCustomValidity(clearValidation);' value='#=Discount.ParsedValue #'>")
                                    .HtmlAttributes(new Dictionary<string, object> { { "style", "text-align: center;" } })
                                    .Title("Discount");
                                columns.Bound(x => x.DepartmentId)
                                    .HtmlAttributes(new { @class = "templateCell" })
                                    .ClientTemplate(Html.Kendo().DropDownList().Name("Department#=LineId#").BindTo(Model.Departments).Value("#= DepartmentId #").ToClientTemplate().ToHtmlString())
                                    .Title("Department");
                                columns.Bound(x => x.SaleDateTime)
                                    .HtmlAttributes(new { @class = "templateCell" })
                                    .ClientTemplate(Html.Kendo().DateTimePicker().Name("SaleDateTime#=LineId#").Value("#= ConvertedSaleDateTime #").ToClientTemplate().ToHtmlString())
                                    .Title("Sale Date");
                                columns.Bound(x => x.SellerId)
                                    .HtmlAttributes(new { @class = "templateCell" })
                                    .ClientTemplate(Html.Kendo().DropDownList().Name("Seller#=LineId#").BindTo(Model.Sellers).Value("#= SellerId #").ToClientTemplate().ToHtmlString())
                                    .Title("Seller");
                                columns.Bound(x => x.IsCashPayment)
                                    .ClientTemplate("<input type='checkbox' id='IsCashPayment#=LineId#' checked='#= IsCashPayment.ParsedValue #' class='regular-checkbox'/><label for='IsCashPayment#=LineId#'></label> Yes")
                                    .Title("Is Cash Payment");
                            })
                            )

在使用&#34;#= value#&#34;的所有列中;工作得很好但不在这一行

.ClientTemplate(Html.Kendo().DateTimePicker().Name("SaleDateTime#=LineId#").Value("#= ConvertedSaleDateTime #").ToClientTemplate().ToHtmlString())

&#34;#= ConvertedSaleDateTime#&#34;没有改变实际价值,但如果我写

.ClientTemplate("#= ConvertedSaleDateTime #")

我会得到正确的价值&#34; 10/07/2013 13:15&#34;。如果我写

.ClientTemplate(Html.Kendo().DateTimePicker().Name("SaleDateTime#=LineId#").Value("10/07/2013 13:15").ToClientTemplate().ToHtmlString())

我将在网格中获得Kendo DateTimePicker的值&#34; 10/07/2013 13:15&#34; 如何从ConvertedSaleDateTime为此DateTimePicker设置值? 请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我通过jQuery解决了我的问题。也许有人需要这个解决方案或知道更美丽的东西。 在SaleDateTime columnt的客户端模板中,我写道:

columns.Bound(x => x.SaleDateTime).ClientTemplate("<input class='saleDateTimeEditor' id='SaleDateTime#=index(data)#' name='ImportedSalesList[#=index(data)#].SaleDateTime' value='#= ConvertedSaleDateTime #'>")

在我的剑道网格的DataBound事件中,我初始化了所有kendo DateTimePickers:

    $('.saleDateTimeEditor').each(function () {
        var id = $(this).attr('id');
        var value = new Date(Date.parse($(this).val()));
        $("#" + id).kendoDateTimePicker({
            value: value,
            max: new Date(Date.now())
        });
        $('#'+id).attr('readonly', 'readonly');
    });

ConvertedSaleDateTime格式为“yyyy / MM / dd hh:mm:ss”