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

Access to date field

4 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Koren
Top achievements
Rank 1
Koren asked on 19 Mar 2015, 01:26 PM
I am trying to access a date field from the grid to pass to a dropdown filter read action.  However, all I get is null in the results.  If I debug, I can see the date is the long version of 1/1/2013 which is correct but the value ends up null.  What am I doing wrong?

VIEWMODEL
[Display(Name = "In Service Date")]
[DataType(DataType.Date)]
[Required]
public DateTime InServiceDate { get; set; }

VIEW  (correctly displays 01/01/2013 in the grid)
columns.Bound(p => p.InServiceDate).Format("{0:MM/dd/yyyy}").Title("Date").Width(100);
 
model.Field(p => p.InServiceDate).DefaultValue(Model.DefaultInServiceDate);

Filter for dropdown (other fields work fine but date field is always null)
function filterTaxClassBookMethods() {       
        var row = $("#TaxClassBookMethod").closest("tr");
        var grid = $("#TaxClassBookMethod").closest("[data-role=grid]").data("kendoGrid");
        var dataItem = grid.dataItem(row);
 
        var inServiceDate = dataItem.InServiceDate;
 
        return { depreciationBookID: 1002, assetClassID: dataItem.AssetTransactionCode.AssetClassID, inServiceDate: inServiceDate };
    }

4 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 23 Mar 2015, 09:51 AM
Hello Koren,

From the provided information it's not clear for us what is the exact configuration of the Grid that you are using - could you please provide the complete Grid definition (as well as the Model and related editors)? This would help us get better overview of the exact setup that you have. 
Also with current implementation the "TaxClassBookMethod" editor should be visible in order to get the current row dataItem - could you please make sure this editor is available when the "filterTaxClassBookMethods" method is executed?

Regards,
Vladimir Iliev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Koren
Top achievements
Rank 1
answered on 23 Mar 2015, 01:29 PM
Note that the other fields from the dataItem work fine.  I get the TaxMethodPreference and AssetClassID without a problem.  I also tried AssetNbr just to see if it worked and it does.  It is only the date field that I can't seem to figure out how to pass.

This is the viewmodel:
public class AddTransactionViewModel
   {
        public int AddTransactionID { get; set; }
        public int FarmID { get; set; }
        public int Year { get; set; }
 
        [Display(Name = "Asset Nbr")]
        [Required]
        public string AssetNbr { get; set; }
 
        [UIHint("AssetTransactionCodeEditor")]
        [Required]
        public KMAR105.Data.Depr.AssetTransactionCode AssetTransactionCode { get; set; }              
 
        [UIHint("TaxClassBookMethodEditor")]
        [Required]
        public KMAR105.Data.Depr.ClassBookMethod TaxClassBookMethod { get; set; }
 
        [UIHint("BonusDepreciationRateEditor")]
        [Required]
        public KMAR105.Data.Depr.DepreciationRate BonusDepreciationRate { get; set; }       
 
        [Display(Name = "Description")]
        [Required]
        public string Description { get; set; }
 
        [Display(Name = "In Service Date")]
        [DataType(DataType.Date)]
        [Required]
        public DateTime InServiceDate { get; set; }
 
        [Display(Name = "Cost Basis")]              
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double? OriginalCost { get; set; }
 
        [Display(Name = "Asset Type")]
        [Required]
        public int AssetTypeID { get; set; }
 
        [Display(Name = "Bus %")]       
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:P2}")]
        [Range(0.0, 1.0)]
        public double BusinessPercent { get; set; }
 
        [Display(Name = "Qty")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double OriginalQuantity { get; set; }
 
        [Display(Name = "Sect 179")]       
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double? ExpenseDeduction { get; set; }
 
        [Display(Name = "Bonus")]
        public double TakeBonusDepr { get; set; }
 
        [Display(Name = "Related Asset Nbr")]
        public string RelatedAssetNbr { get; set; }
 
        [Display(Name = "Comment")]
        public string Comment { get; set; }
 
        [Display(Name = "Units Use")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#,###,##0.00}")]
        [Range(0.0, 9999999.99)]
        public double? UnitsUse { get; set; }
 
        [Display(Name = "Previously Purchased")]
        public bool PreviouslyPurchased { get; set; }
 
        [Display(Name = "Prior Depr")]       
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double? PriorDepreciation { get; set; }
 
        [Display(Name = "Salvage")]       
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double? SalvageValue { get; set; }
 
        [Display(Name = "Avg Conv")]
        public int AveragingConventionID { get; set; }
 
        [Display(Name = "Depr Meth")]
        public int DepreciationMethodID { get; set; }
 
        [Display(Name = "Life")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double RecoveryPeriod { get; set; }
 
        [Display(Name = "Mgmt Basis")]       
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###,###,##0.00}")]
        [Range(0.0, 999999999.99)]
        public double? MgmtBasis { get; set; }
 
        [Display(Name = "Mgmt Class Book Method")]
        public int MgmtClassBookMethodID { get; set; }
 
        [Display(Name = "Orig Tax Class Book Method")]
        public int OrigTaxClassBookMethodID { get; set; }      
 
        [Display(Name = "Transfer Date")]
        [DataType(DataType.Date)]
        public DateTime TransferDate { get; set; }       
 
        [Display(Name = "1st Yr Conv")]
        public string TaxAveragingConvention { get; set; }
 
        [Display(Name = "Tax Meth")]
        public string TaxDepreciationMethod { get; set; }       
 
        [Display(Name = "Last Person")]
        public string LastPerson { get; set; }
 
        [Display(Name = "Tax Pref")]
        public string TaxMethodPreference { get; set; }
             
 
    }



This is the grid in the view:
@(Html.Kendo().Grid<KMAR105.MVC.Depr.Models.AddTransactionViewModel>()
            .Name("addTranGrid")
            .Columns(columns =>
            {
                columns.Command(command => command.Destroy().Text("Dlt")).Width(100);               
                columns.Bound(p => p.AssetNbr).Title("#").Width(70)
                    .ClientFooterTemplate("Cnt: #=count#");
                columns.Bound(p => p.AssetTransactionCode).Title("ATIGD").Width(100).ClientTemplate("#: data.AssetTransactionCode != null ? AssetTransactionCode.Code : '' #");
                columns.Bound(p => p.Description).Title("Description").Filterable(false).Width(210);
                columns.ForeignKey(p => p.AssetTypeID, @Model.AssetTypes, "DVID", "DomainValue")
                   .Title("New?").Width(100);
                columns.Bound(p => p.BusinessPercent).Title("Bus %").Filterable(false).Width(90);
                columns.Bound(p => p.OriginalQuantity).Title("Qty").Filterable(false).Width(90)
                    .HtmlAttributes(new { @class = "text-right" });
                columns.Bound(p => p.InServiceDate).Title("Date").Width(100);
                columns.Bound(p => p.OriginalCost).Title("Cost Basis").Filterable(false).Width(100)
                    .ClientFooterTemplate("Total: #= kendo.format('{0:C2}',sum)#")
                    .HtmlAttributes(new { @class = "text-right" });
                columns.Bound(p => p.ExpenseDeduction).Title("Sect 179").Filterable(false).Width(100)
                    .ClientFooterTemplate("Total: #= kendo.format('{0:C2}',sum)#")
                    .HtmlAttributes(new { @class = "text-right" });
                columns.Bound(p => p.BonusDepreciationRate).Title("Bonus").Width(140).ClientTemplate("#: data.BonusDepreciationRate != null ? BonusDepreciationRate.Rate : '0.00' #");
                columns.Bound(p => p.TaxClassBookMethod).Title("Tax Method").Width(140).ClientTemplate("#: data.TaxClassBookMethod != null ? TaxClassBookMethod.Description : '' #");
                columns.ForeignKey(p => p.DepreciationMethodID, @Model.DepreciationMethods, "DVID", "DomainValue")
                   .Title("Depr Meth").Filterable(false).Width(115);
                columns.Bound(p => p.RecoveryPeriod).Title("Life").Filterable(false).Width(90);
                columns.Bound(p => p.UnitsUse).Title("Units Use").Filterable(false).Width(85)
                    .HtmlAttributes(new { @class = "text-right" });
                columns.Bound(p => p.Comment).Title("Comments").Filterable(false).Width(150);
                columns.Bound(p => p.RelatedAssetNbr).Title("Rel Ast #").Filterable(false).Width(80);
                columns.Bound(p => p.AveragingConventionID).Title("1st Yr Conv").Hidden(true).Width(80);
                columns.Bound(p => p.TaxMethodPreference).Title("TaxPref").Hidden(true).Width(80);
            })
            .ToolBar(toolbar =>
            {
                toolbar.Create();
                toolbar.Save();
            })
            .Editable(editable => editable.Mode(GridEditMode.InCell))
            .Navigatable()
            .Sortable()
            .Scrollable(scr => scr.Height(650))
            .Filterable()
            .Events(events => events.Edit("grid_OnEdit"))
            .DataSource(dataSource => dataSource
                 .Ajax()
                 .Batch(true)
                 .ServerOperation(false)
                 .Events(events => events.Error("error_handler"))
                 .Aggregates(aggregates =>
                    {
                        aggregates.Add(p => p.AssetNbr).Count();
                        aggregates.Add(p => p.OriginalCost).Sum();
                        aggregates.Add(p => p.ExpenseDeduction).Sum();
                    })
                 .Model(model =>
                 {
                     model.Id(p => p.AddTransactionID);
                     //model.Field(p => p.AssetNbr).DefaultValue(Model.MaxAssetNbr);
                     model.Field(p => p.FarmID).DefaultValue(Model.FarmID);
                     model.Field(p => p.Year).DefaultValue(Model.Year);
                     model.Field(p => p.AssetTypeID).DefaultValue(Model.DefaultAssetTypeID);
                     model.Field(p => p.InServiceDate).DefaultValue(Model.DefaultInServiceDate);
                     model.Field(p => p.OriginalCost).DefaultValue(0.0);
                     model.Field(p => p.ExpenseDeduction).DefaultValue(0.0);
                     model.Field(p => p.TakeBonusDepr).DefaultValue(0.0);
                     model.Field(p => p.UnitsUse).DefaultValue(0.0);
                     model.Field(p => p.PreviouslyPurchased).DefaultValue(false);
                     model.Field(p => p.BusinessPercent).DefaultValue(1.0);
                     model.Field(p => p.OriginalQuantity).DefaultValue(1.0);
                     model.Field(p => p.AssetTransactionCode).DefaultValue(new KMAR105.Data.Depr.AssetTransactionCode() { Code = "" });
                     model.Field(p => p.TaxClassBookMethod).DefaultValue(new KMAR105.Data.Depr.ClassBookMethod() { Description = "" });
                     model.Field(p => p.BonusDepreciationRate).DefaultValue(new KMAR105.Data.Depr.DepreciationRate() { Rate = 0.00 });
                     model.Field(p => p.PriorDepreciation).Editable(false).DefaultValue(0.0);
                     model.Field(p => p.SalvageValue).Editable(false).DefaultValue(0.0);
                     model.Field(p => p.AveragingConventionID).Editable(false).DefaultValue(0);
                     model.Field(p => p.DepreciationMethodID).Editable(true).DefaultValue(0);
                     model.Field(p => p.RecoveryPeriod).Editable(true).DefaultValue(0.0);
                     model.Field(p => p.TransferDate).Editable(false).DefaultValue(Model.DefaultTransferDate);
                     model.Field(p => p.TaxMethodPreference).Editable(false).DefaultValue(Model.TaxMethodPreference);
                 })
                 .Read(read => read.Action("New_Read_AddTransactions", "AddTransaction", new { year = Model.Year, farmID = Model.FarmID }))
                 .Create("New_Editing_Create", "AddTransaction", new { year = Model.Year, farmID = Model.FarmID })
                .Update("New_Editing_Update", "AddTransaction")
                .Destroy("New_Editing_Destroy", "AddTransaction")
            )
        )


This is the editor:
@using xxx.Models
@using System.Collections
 
<script>
    function filterTaxClassBookMethods() {
        //var row = $(event.srcElement).closest("tr");
        //var grid = $(event.srcElement).closest("[data-role=grid]").data("kendoGrid");
        var row = $("#TaxClassBookMethod").closest("tr");
        var grid = $("#TaxClassBookMethod").closest("[data-role=grid]").data("kendoGrid");
        var dataItem = grid.dataItem(row);
         
        var inServiceDate = dataItem.InServiceDate;
 
        return { taxMethodPreference: dataItem.TaxMethodPreference, depreciationBookID: 1002, assetClassID: dataItem.AssetTransactionCode.AssetClassID, inServiceDate: inServiceDate };
    }
</script>
@(Html.Kendo().DropDownList()
    .Name("TaxClassBookMethod") // Name of the widget should be the same as the name of the property
    .DataValueField("ClassBookMethodID") // The value of the dropdown
    .DataTextField("Description") // The text of the items   
    .DataSource(dataSource =>
    {
        dataSource.Read(read => read.Action("GetClassBookMethods", "AddTransaction").Data("filterTaxClassBookMethods")).ServerFiltering(true);
    })
   .Events(e => e.Change("taxclassbookmethod_changed").DataBound("function(){this.trigger('change')}"))  
)

0
Accepted
Vladimir Iliev
Telerik team
answered on 24 Mar 2015, 09:10 AM
Hi Koren,

I inspected the provided code and everything looking valid. Could you please clarify if the issue that you are experiencing is binding the data on the server side? If this is the case than you can try formatting the date before sending it to the server side as follows:

kendo.format("{0:G}", dataItem.InServiceDate);

Regards,
Vladimir Iliev
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Koren
Top achievements
Rank 1
answered on 24 Mar 2015, 02:37 PM
That worked.  Thank you!
Tags
Grid
Asked by
Koren
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Koren
Top achievements
Rank 1
Share this question
or