This is a migrated thread and some comments may be shown as answers.

Read-only DateTime column on creation/update

1 Answer 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brian Vallelunga
Top achievements
Rank 1
Brian Vallelunga asked on 27 Mar 2013, 04:51 PM
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?

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 29 Mar 2013, 02:22 PM
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!
Tags
Grid
Asked by
Brian Vallelunga
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or