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);
}
});
});
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);
}
});
});