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

[Solved] Nullable DateTime/Date template cannot update non-null value with the null(empty) one

2 Answers 30 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Victoria
Top achievements
Rank 1
Victoria asked on 20 Mar 2012, 10:56 AM
ASP.NET MVC 3
OS: Windows
Browser: Google Chrome 17.0.963.56 m
Telerik: Extensions for ASP.NET MVC Q1 2012
Preferred Language: C# , Razor

Hello,

i am using a nullable Date in my Model:
[DataType(DataType.Date)]
public DateTime? BirthDate { get; set; }
with the following template 
@model DateTime?
 
 
@(Html.Telerik().DatePickerFor(m => m)
      .HtmlAttributes(new { id = ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty) + "_wrapper" })
)

The problem occures when I try to update the date from non-null value (e.g. 8/21/1972) to the empty one. In this case after update the previous non-null value is set instead of the empty one.
Here is the update method in controller:
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _Update(int id)
{
    var customer = SessionRepository.One(p => p.Id == id);
 
    TryUpdateModel(customer);
    SessionRepository.Update(customer);
    return View(new GridModel(SessionRepository.All()));
}

Please, find the sample project attached. Steps to reproduce: try to update any birth date to null (clear it up) and save it.

Please, advise.

Thank you,
Victoria

2 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 23 Mar 2012, 09:59 AM
Hello Victoria,

If the date field is empty then no value is sent to the server. Also the TryUpdateModel method does not update the BirthDate if there is no value provided for that field. 
In such case you can use the following approach:
[AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _Update(Customer cust)
        {
            var customer = SessionRepository.One(p => p.Id == cust.Id);
 
            TryUpdateModel(customer);
            if (cust.BirthDate==null)
            {
                customer.BirthDate = null;
            }
            SessionRepository.Update(customer);
            return View(new GridModel(SessionRepository.All()));
        }


Kind regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Victoria
Top achievements
Rank 1
answered on 23 Mar 2012, 01:55 PM
Hi Petur,

Thank you for your help and quick reply.
Everything is working fine now.

Regards,
Victoria

Tags
Grid
Asked by
Victoria
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Victoria
Top achievements
Rank 1
Share this question
or