MVC4剑道网格DateTime模板编辑器



我有这个型号:

public class MainModel 
{
private Guid m_id;
        public Guid Id
        {
            get { return m_id; }
            set { m_id = value; }
        }
        private string m_name;
        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }
private ObservableCollection<Foundation> m_foundations;
        public ObservableCollection<Foundation> Foundations
        {
            get 
            {
                if (m_foundations== null)
                    m_foundations= new ObservableCollection<Foundation>();
                return m_foundations;
            }
            set { m_foundations= value; }
        }
}
public class Foundation
{
private ObservableCollection<Worker> m_workers;
        public ObservableCollection<Worker> Workers
        {
            get 
            {
                if (m_workers == null)
                {
                    m_workers = new ObservableCollection<Worker>();
                }
                return m_workers; 
            }
            set { m_workers = value; }
        }
}
public class Worker
{
[HiddenInput(DisplayValue = false)]
        public Guid Id
        {
            get { return m_id; }
            set { m_id = value; }
        }
[DataType(DataType.DateTime), DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime BirthDate
        { get; set; }
}

我有一个部分视图,它接收这个主模型:

@using (Html.BeginForm("Save", "Controller", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl }))
    {
        @Html.TextBoxFor(m => m.Name)
        @Html.HiddenFor(m => m.Id)
<div id="workers">
@(Html.Kendo().Grid(Model.Foundations[0].Workers)
 .Name("grid")
                .Columns(columns =>
                {
                    columns.Command(command => { command.Edit().Text("Edit"); command.Destroy().Text("Delete"); }).Width(200);
                    columns.Bound(p => p.Name);
                    columns.Bound(p => p.BirthDate).Width(100).Format("{0: dd-MM-yyyy}").EditorTemplateName("BirthDate");
})
.Editable(editable => editable.Mode(GridEditMode.InLine).CreateAt(GridInsertRowPosition.Bottom))
.DataSource(dataSource => dataSource
                    .Ajax()
                    .PageSize(20)
                    .Model(model => model.Id(p => p.Id))
                    .Create(update => update.Action("EditingInline_Create", "Controller"))
                    .Update(update => update.Action("EditingInline_Update", "Controller"))
                    .Destroy(update => update.Action("EditingInline_Destroy", "Controller"))
                    ))
</div>

出生日期编辑器有类似于的内容

@model DateTime
@(
    Html.Kendo().DatePickerFor(m=>m).Name("birthDatePicker")
)

该模型对所有属性和日期时间都很好。问题是:model。Foundations[0]。Workers[]。BirthDate有一个有效的日期,但在模板编辑器中给了我minvalue模型的日期时间。

当我编辑该列时,显示的值将类似于01-01-0001,而不是15-03-2006。好的,我只是更改了日期,但当我单击更新时,DateTime的值总是DateTime.Now。在EditingInline_Update中,参数worker将BrithDate更改为DateTime。现在…我更改的值不存在。

我在某个地方读到这可能是一个文化形成问题,但我完全按照他们说的做了:创建了一个DateModelBinder并在global.asax中注册,但在调试时,传递的值是DateTime。现在…它已经在那里更改了。Web.config具有我更改了线程的当前文化和UIculture,更改了kendo.culture,签入调试,一切似乎都是正确的。我甚至在编辑器中放入了javascript验证器:

$("#birthDatePicker").kendoValidator({
        rules: {
            date: function (input) {
                var d = kendo.parseDate(input.val(), "dd-MM-yyyy");
                return d instanceof Date;
            }
        }
    });

这返回true,在这里我可以看到我想要更改的日期,但当转到modelbinder时,它不知何故更改为DateTime.now

我很抱歉这么长时间,但这让我疯了,我不知道这是因为集合中有集合的错误,还是我错过了其他东西。提前Thanx

我的一个项目中有一个MVC kendo ui网格,它在编辑模式下可以很好地处理日期。我注意到与您的代码不同的是,我没有在模型中的DateTime属性上写入任何属性。我的模型看起来很简单:

public class TransactionViewModel
{
    [Key]
    public int TransactionId { get; set; }
    public string Description { get; set; }
    [Required]
    public DateTime TransactionDate { get; set; }
    [Required]
    public decimal Amount { get; set; }
}

在网格中,我以以下方式绑定列:

columns.Bound(p => p.TransactionDate).Title("Date").Format("{0:yyyy-MM-dd}").EditorTemplateName("TransactionDate");

而且,尽管我的编辑器模板看起来和你的有点不同,但当我测试用你的模板替换我的模板时,你提供的代码运行得很好。

@model DateTime?
@(Html.Kendo().DatePicker()
    .Name("TransactionDate")
    .Value(Model == null ? DateTime.Now.Date : ((DateTime)@Model).Date)
)

为了处理文化问题,我还添加了以下代码:

@{
    var culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
}
<script src="@Url.Content("~/Scripts/cultures/kendo.culture." + culture + ".min.js")"></script>
<script>
    kendo.culture("@culture");
</script>

我建议你尝试以下方法:

  • 删除模型中的属性以验证它们是否不会导致任何问题。你可以在以后一切正常时添加它们
  • 请尝试指定其他日期格式,或者不指定日期格式。如果指定了一种格式,而用户的浏览器希望以另一种格式呈现,则可能会遇到麻烦

最新更新