Disable ajax validation for Created and Modified Dates

5 posts, 1 answers
  1. Greg
    Greg avatar
    20 posts
    Member since:
    May 2012

    Posted 13 Mar 2013 Link to this post

    I have a base class that all other entities derive from which has created and modified dates. I want to automatically enter the dates based off logic which determines if the entity is being updated or inserted:

    public override int SaveChanges()
    {
    DateTime now = DateTime.Now;
    foreach (var entity in ChangeTracker.Entries<Base>()
    .Where(e => e.State == EntityState.Added)
    .Select(e => e.Entity))
    {
    entity.ModifiedOn = now; // set the date
    entity.CreatedOn = now;
    }
    foreach (var entity in ChangeTracker.Entries<Base>()
    .Where(e => e.State == EntityState.Modified)
    .Select(e => e.Entity))
    {
    entity.ModifiedOn = now; // set the date
    }

    return base.SaveChanges();
    }

    I want to show the fields in the grid as read only, but the grid is doing ajax validation on these fields on edit and insert and I get an error "The field ModifiedOn must be a date." I want to disable the validation for just these fields. Here is the rest of the code below:

    public abstract class Base
    {
    //[HiddenInput(DisplayValue = false)]
    //[ReadOnly(true)]
    //[DataType(DataType.Text)]
    //[UIHint("DateTime")]
    //[Display(Name = "Created On")]
    //[DisplayFormat(DataFormatString = "{0:d}")]
    [HiddenInput(DisplayValue = false)]
    [DataType(DataType.Text)]
    public DateTime CreatedOn { get; set; }
    [Display(Name = "Created By")]
    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public int CreatedBy { get; set; }
    [HiddenInput(DisplayValue = false)]
    [DataType(DataType.Text)]
    public DateTime ModifiedOn { get; set; }
    [Display(Name = "Modified By")]
    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public int ModifiedBy { get; set; }
    }

    public class Parameter : Base
    {
    [Key]
    [ScaffoldColumn(false)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ParameterID { get; set; }
    public string Name { get; set; }
    public int Numeric { get; set; }
    public string String { get; set; }
    public string Description { get; set; }
    }

    <div>
    <input type="button" visible="false" value="Insert" id="btnInsert">
    <input type="button" visible="false" value="Edit" id="btnEdit">
    <input type="button" visible="false" value="Save" id="btnSave">
    <input type="button" visible="false" value="Delete" id="btnDelete">
    </div>
    <h2>Parameter Lookup Configuration</h2>
    <br />
    <br />
    @(Html.Kendo().Grid<ECRI.Phoenix.Core.Models.Parameter>()
    .Name("grid")
    .Columns(columns =>
    {
    columns.Template(@<text></text>).ClientTemplate("<input type='checkbox' id='chkbx' class='chkbx' />")
    .HeaderTemplate("<input type='checkbox' id='masterCheckBox' />").Width(30);
    columns.Bound(p => p.Name);
    columns.Bound(p => p.String);
    columns.Bound(p => p.Numeric);
    columns.Bound(p => p.Description);
    columns.Bound(p => p.ModifiedBy);
    columns.Bound(p => p.CreatedOn).Format("{0:d}");
    columns.Bound(p => p.ModifiedOn).Format("{0:d}");
    })
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Resizable(resize => resize.Columns(true))
    .Reorderable(reorder => reorder.Columns(true))
    .Groupable()
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))
    .DataSource(dataSource => dataSource
    .Ajax()
    .Model(model => model.Id(p => p.ParameterID))
    .Read("Get", "Parameter")
    .Update("Update", "Parameter")
    .Create("Insert", "Parameter")
    .Destroy("Delete", "Parameter"))
    )

    <script src="@Url.Content("~/Scripts/Phoenix.js")"></script>

    $(document).ready(function () {
    $("#btnDelete").click(function () {
    var grid = $("#grid").data("kendoGrid");
    var checked = false;
    $.each($('#grid :checkbox:checked').closest('tr'), function () {
    grid.removeRow($(this)); //just gives alert message
    });
    });
    $("#btnEdit").click(function () {
    var grid = $("#grid").data("kendoGrid");
    grid.select().each(function () {
    grid.editRow($(this)); //just gives alert message
    });
    });
    $("#btnInsert").click(function () {
    var grid = $("#grid").data("kendoGrid");
    grid.addRow();
    });
    $('#masterCheckBox').click(function () {
    if (this.checked)
    $("#grid tbody input:checkbox").attr("checked", this.checked);
    else {
    $("#grid tbody input:checkbox").removeAttr("checked", this.checked);
    }
    });
    });
  2. Answer
    Daniel
    Admin
    Daniel avatar
    2219 posts

    Posted 15 Mar 2013 Link to this post

    Hello Greg,

    The validation is added automatically by MVC and cannot be disabled. You could either use the ScaffoldColumnAttribute instead of HiddenInputAttribute. If the column should be shown in the Grid you can use the Visible method e.g.

    [ScaffoldColumn(false)]
    [DataType(DataType.Text)]
    public DateTime ModifiedOn { get; set; }
    columns.Bound(p => p.ModifiedOn).Visible(true).Format("{0:d}");

    Alternative solutions are to use a custom popup editor template without inputs for the dates or to remote the data-val-date attribute in the Grid edit event. 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!
  3. Greg
    Greg avatar
    20 posts
    Member since:
    May 2012

    Posted 16 Mar 2013 Link to this post

    Thank you Daniel! This is exactly what I was looking for and was banging my head for days over this. I really appreciate the help!
  4. Greg
    Greg avatar
    20 posts
    Member since:
    May 2012

    Posted 05 Apr 2013 Link to this post

    Daniel,

    This works great for popup mode, but when I try to change it to inline editing it gives me the same error. I added javascript to remove the data-val-date attribute but still no luck. Any ideas on this?
  5. Daniel
    Admin
    Daniel avatar
    2219 posts

    Posted 09 Apr 2013 Link to this post

    Hello again Greg,

    For InLine and InCell editing you could specify that the field should not be editable through the DataSource model:

    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model =>
            {
                model.Id(p => p.ParameterID);
                model.Field(p => p.ModifiedOn).Editable(false);
            })
    or with the Editable attribute. 

    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