BootStrap Datepicker return null from View to Controller

ManiMaranVasan

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

Daniel J.G.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

BootStrap Datepicker return null from View to Controller

From Dev

jQuery datepicker with Spring MVC - Passing date from view to controller sends null

From Dev

Return a partial view from a controller?

From Dev

How to return empty view in controller if string is not null?

From Dev

Cannot return a view from a different controller in OnActionExecuting

From Dev

Codeigniter return a message to view from controller trouble

From Dev

Return logic from Controller into View Laravel 5

From Dev

How to return the view from controller in laravel in this case

From Dev

How to change UI Bootstrap Datepicker from controller to directive

From Dev

How to pass data from datepicker in view to controller in mvc

From Dev

Passing JSON data from View to Controller is null

From Dev

ajax posting from a partial view to controller is null

From Dev

findViewById() from custom view return null

From Dev

How to pass the value return from javascript from view to controller in laravel

From Dev

Return the result from inner join linq from controller to view

From Dev

How to post time using bootstrap datetimepicker from view to controller

From Dev

how to return two variables from controller to view in mvc3

From Dev

MVC 4 - Return error message from Controller - Show in View

From Dev

Calling a Web API Controller from an MVC View to return an XML file

From Dev

need to call controller method which return String value from view

From Dev

Is it mandatory to return the view name from controller in spring mvc?

From Dev

Calling a Web API Controller from an MVC View to return an XML file

From Dev

Calling function from the same controller to return the view with flash message

From Dev

Should I return only Json Objects from controller to view?

From Dev

Lang in laravel not work in controller from ajax return to view

From Dev

how to return multiple variable with jsonresult in mvc from controller to view?

From Dev

why label in view get data from controller is null

From Dev

How to pass multiple jquery datepicker parameters from view to controller in MVC 5?

From Dev

JQuery Datepicker in Grails, controller receiving null

Related Related

  1. 1

    BootStrap Datepicker return null from View to Controller

  2. 2

    jQuery datepicker with Spring MVC - Passing date from view to controller sends null

  3. 3

    Return a partial view from a controller?

  4. 4

    How to return empty view in controller if string is not null?

  5. 5

    Cannot return a view from a different controller in OnActionExecuting

  6. 6

    Codeigniter return a message to view from controller trouble

  7. 7

    Return logic from Controller into View Laravel 5

  8. 8

    How to return the view from controller in laravel in this case

  9. 9

    How to change UI Bootstrap Datepicker from controller to directive

  10. 10

    How to pass data from datepicker in view to controller in mvc

  11. 11

    Passing JSON data from View to Controller is null

  12. 12

    ajax posting from a partial view to controller is null

  13. 13

    findViewById() from custom view return null

  14. 14

    How to pass the value return from javascript from view to controller in laravel

  15. 15

    Return the result from inner join linq from controller to view

  16. 16

    How to post time using bootstrap datetimepicker from view to controller

  17. 17

    how to return two variables from controller to view in mvc3

  18. 18

    MVC 4 - Return error message from Controller - Show in View

  19. 19

    Calling a Web API Controller from an MVC View to return an XML file

  20. 20

    need to call controller method which return String value from view

  21. 21

    Is it mandatory to return the view name from controller in spring mvc?

  22. 22

    Calling a Web API Controller from an MVC View to return an XML file

  23. 23

    Calling function from the same controller to return the view with flash message

  24. 24

    Should I return only Json Objects from controller to view?

  25. 25

    Lang in laravel not work in controller from ajax return to view

  26. 26

    how to return multiple variable with jsonresult in mvc from controller to view?

  27. 27

    why label in view get data from controller is null

  28. 28

    How to pass multiple jquery datepicker parameters from view to controller in MVC 5?

  29. 29

    JQuery Datepicker in Grails, controller receiving null

HotTag

Archive