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

Date issue

0 Answers 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ankit
Top achievements
Rank 1
ankit asked on 25 Jun 2013, 03:15 AM
Hi there

We are using license version of KendoUI version 2012.2.913.340.
It's MVC 4 in .Net 4.5.

I have a grid on a page and it has CRUD on controller. I do get data on grid as a search result.

When I edit one of the record and try to save, I get issue on date as I see date coming to controller is : 1/01/0001 12:00:00 AM. I tried to put following code to see what the issue is and I can see issue is on line 16 where Attempted value is 6/25/2013 10:45:00 AM in place of 25/6/2013 10:45:00 AM.

FYI,
I do have web.config set to following:
<globalization culture="en-AU" uiCulture="en-AU" />
I have tried using Code first 5.0 as well as EDMX (default)
public class MyDateTimeModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {


            var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (!string.IsNullOrEmpty(displayFormat) && value != null)
            {
                DateTime date;
                displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);
                // use the format specified in the DisplayFormat attribute to parse the date

                bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName));

                if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    return date;
                }
                else
                {
                    bindingContext.ModelState.AddModelError(
                        bindingContext.ModelName,
                        string.Format("{0} is an invalid date format", value.AttemptedValue)
                    );
                }
            }

            return base.BindModel(controllerContext, bindingContext);
        }


    }

I have created controller (on a same site), as how Microsoft does by right clicking on controller right clicking on view etc.. and it has generated CRUD which works without any issue. I mean above all line 30 works with correct date format.

Can you please tell me what I am doing wrong here.

I am in Australia and like to see en-AU culture.

No answers yet. Maybe you can help?

Tags
Grid
Asked by
ankit
Top achievements
Rank 1
Share this question
or