Read-only DateTime column on creation/update

2 posts, 0 answers
  1. Brian Vallelunga
    Brian Vallelunga avatar
    99 posts
    Member since:
    May 2008

    Posted 27 Mar 2013 Link to this post

    I have a grid that is working well, except that I am unable to create or update a row that contains a date.

    On the back-end, my model class looks like this:
    public class Model
    {
        public Model() {
            this.CreateDate = DateTime.UtcNow;
        }
     
        // Other properties
        public DateTime CreateDate { get; private set; }
    }

    In my MVC cshtml file I have:

    // Other setup...
    .DataSource(ds =>
            ds.Ajax()
            .Model(model =>
                {
                    model.Id(o => o.Id);
                    model.Field(m => m.CreateDate).Editable(false);
                })
    // more...

    I would think that with this code that the data source would not try to pass the CreateDate field at all to the server, since it is read only. This appears not to be the case and it passes something that MVC doesn't understand such as "Wed Mar 27 2013 16:41:40 GMT-0400 (Eastern Daylight Time)." How do I prevent it from sending this value?
  2. Daniel
    Admin
    Daniel avatar
    2219 posts

    Posted 29 Mar 2013 Link to this post

    Hello,

    The dataSource always sends all fields from the model but you could use the ReadOnlyAttribute to prevent the value from being bound by the model binder:

    public class Model
    {
        public Model() {
            this.CreateDate = DateTime.UtcNow;
        }
      
        [ReadOnly(true)]
        public DateTime CreateDate { get; private set; }
    }
    Kind regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top