ManiMaranVasan
ManiMaranVasan

Reputation: 83

BootStrap Datepicker return null from View to Controller

1st i explain my problem,we are using BootStrap DatePicker, after submit the Html.BeginForm it goes to controller with select date and updating the database in my development system(pc) but the same coding is not working in testing server and client system. After debug on Testing server i found the datepicker return NULL to controller when i choose the Date more than 12 Day e.g (13-10-2014) and for less then 12 date like (12-10-2014) is working fine.

I changed the culture as Invariant but still its return null value to controller

The datepicker defined inside the BeginForm, Other controls are returning the correct values expect the DatePicker.

Html.BeginForm("MergeTeacherDetails","Profile", FormMethod.Post, new { id = "FormTeacherDetails" })))

The DatePicker Format changed as ('dd/mm/yyyy') Here the View Code

@Html.LabelFor(m => m.DOB, "DOB", new { @class = "control-label"})
<div class="controls">
 @{System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
   @Html.TextBoxFor(model => model.DOB, new { placeholder = "DOB",@readonly = "readonly",@class="ReadOnly" })}
</div>

Here the JavaScript

$('#DOB').datepicker({ startView: 2, autoclose: true, format: "dd/mm/yyyy", endDate: '@DateTime.Now.ToString("dd/MM/yyyy")', changeYear: true, changeMonth: true, weekStart: 1 });

Here the DAL code

 public Nullable<System.DateTime> DOB { get; set; }

Here the Controller Code

public ActionResult MergeTeacherDetails(Staffs objStaffs)
    {

         int res = _staffRepository.InsertOrUpdate(objStaffs);
         if(res>0)
         {
          _mapStaffRepository.SaveMapStaff(objStaffs.StaffId);
         }
         return RedirectToAction("/MyProfile/1");
    }

In controller Parameter objStaffs.DOB is returns as NULL

This

Kindly help me to solve this

Thanks in advance

Upvotes: 1

Views: 3721

Answers (1)

Daniel J.G.
Daniel J.G.

Reputation: 34992

You need to make sure the application uses the expected date format when binding the posted values into datetime values using the model binder (when binding the posted values into your objStafss model)

By default the model binder will use the current culture format, which on your machine will probably be dd/mm/yyyy while on the testing/client machines will be mm/dd/yyyy.

You can create your model binder that always expects dates in the format dd/mm/yyyy (which in .net is expressed as dd/MM/yyyy):

public class CustomModelBinder : DefaultModelBinder
{
    protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext,
        PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
    {
        var propertyType = propertyDescriptor.PropertyType;         
        if(propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
        {
            var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (null != providerValue)
            {
                DateTime date;
                if (DateTime.TryParseExact(providerValue.AttemptedValue, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    return date; 
                }                    
            }
        }
        return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }
}

Then you need to tell MVC to use you model binder, for example you can wire it globally for your application in the global.asax Application_Start event by replacing the default binder:

ModelBinders.Binders.DefaultBinder = new CustomModelBinder();

Upvotes: 4

Related Questions