BootStrap Datepicker从视图到控制器返回null [英] BootStrap Datepicker return null from View to Controller

查看:85
本文介绍了BootStrap Datepicker从视图到控制器返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我解释了我的问题,我们使用BootStrap DatePicker,在提交Html.BeginForm之后将其与选择日期一起发送到控制器,并更新了我的开发系统(pc)中的数据库,但是相同的编码在测试服务器中不起作用,并且客户系统.在测试服务器上调试后,我发现datepicker向控制器返回NULL 当我选择日期多于12天(例如(13-10-2014),然后少于12个日期,例如(12-10-2014)可以正常工作时.

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

在BeginForm中定义的日期选择器,其他控件返回的正确值应为DatePicker.

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" })))

DatePicker格式更改为('dd/mm/yyyy') 这里的查看代码

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>

这里有JavaScript

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

这里是DAL代码

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

此处是控制器代码

public ActionResult MergeTeacherDetails(Staffs objStaffs)
    {

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

在控制器参数objStaffs.DOB中返回为NULL

In controller Parameter objStaffs.DOB is returns as NULL

请帮助我解决此问题

预先感谢

推荐答案

使用模型绑定程序将发布的值绑定到datetime值时,您需要确保应用程序使用预期的日期格式(将发布的值绑定到您的objStafss模型)

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)

默认情况下,模型联编程序将使用当前的区域性格式,在您的计算机上,该区域性格式可能是dd/mm/yyyy,而在测试/客户端计算机上,该区域性格式是mm/dd/yyyy.

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.

您可以创建始终期望日期格式为dd/mm/yyyy(在.net中以dd/MM/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);
    }
}

然后,您需要告诉MVC使用模型绑定程序,例如,您可以通过替换默认绑定程序,在global.asax Application_Start事件中为应用程序全局绑定它:

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

这篇关于BootStrap Datepicker从视图到控制器返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆