Question

I have issue with DatePicker! He doesn't want to show data if there is different culture, although i don't care about cultures and the only standard should be dd/MM/yyyy that is it.

But some how in the value i see dd.mm.yyyy or dd-mm-yyyy or mm/dd/yyyy

here is my code:

@model DateTime?

@{
   string valueString = null;
    if (Model != null)
    {
        valueString = ((DateTime)Model).ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    }

}

<div class="form-group@(Html.ValidationErrorFor(m => m, " has-error"))">
    @Html.LabelFor(m => m, new { @class = "col-sm-2 control-label" })
    <div class="col-sm-10">
    @Html.TextBoxFor(m => m, new { id = "datepicker", @placeholder="dd/mm/yyyy", autocomplete = "off", @class="form-control", @type="text", value = valueString})

    @Html.ValidationMessageFor(m => m, null, new { @class="help-block" })
    </div>
</div>
<script type="text/javascript">

    $(document).ready(function () {
        /*$.datepicker.setDefaults($.datepicker.regional['fr']);*/
        $("#datepicker").datepicker({
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            minDate: '-80Y',
            maxDate: '-17Y',
            yearRange: '-80:-17',

        });
        $("#datepicker").datepicker("option", "showAnim", "slideDown");
    });
</script>

Here is my model:

[Required(ErrorMessageResourceName = "BirthdateRequired", ErrorMessageResourceType = typeof(Resources.Base))]
[Display(Name = "Birthdate", ResourceType = typeof(Resources.Base))]
[DataType(DataType.Date)]
[DateRangeAttribute(FirstDateYears = 80, SecondDateYears = 18, ErrorMessageResourceName = "BirthdateAgeConstraint", ErrorMessageResourceType = typeof(Resources.Base))]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime Birthdate { get; set; }

Could anyone assist?

I found here some queestion regarding this, but they were not answered.

Was it helpful?

Solution

Don't know if it is a good solution, but I've solved this by replacing textboxfor for textbox helper:

@Html.TextBox("",valueString, new{ id = "datepicker", @placeholder="dd/mm/yyyy", autocomplete = "off", @class="form-control", @type="text"})

Hope it could help anyone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top