开发者

jquery datetime picker datetime format issue with MVC3

开发者 https://www.devze.com 2023-04-11 22:42 出处:网络
I\'m using JQuery Date Time Picker in a MVC3 Application.I use date time to filter result for a list.following is how my search URL looks like

I'm using JQuery Date Time Picker in a MVC3 Application.I use date time to filter result for a list.following is how my search URL looks like

/Objectives?Description=&FromDate=03%2F10%2F2011

the selected datetime format is 03 October 2011.

but when it resolved in the controller it is taken as 10 march 2011.

selected culture is en-GB.

but when it used in a ajax form this issue does not exist because Ajax URL is automatically set as /Objectives?Description=&FromDate=10%2F03%2F2011

I use following lines of code to format the date time in the DateTimePicker.

$(function () {
    $(".datePicker").datepicker({ 
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd/mm/yy',
            altField: 'dd-mm-yy', 
            altFormat: 'dd/mm/yy' 

    });
});

following is the code of the editor for the DateTime

@model DateTime?
@{
    var htmlAttributes = new Dictionary<string, Object>();
    htmlAttributes.Add("class", "datePicker");
    htmlAttributes.Add("readonly", "true"); 
}
@Html.TextBox(string.Empty, (Model.HasValue ? Model.Value.ToString() : string.Empty), htmlAttributes)

we use the following model to get data from the user.

public class LogModel : LogBaseModel
{

    [Display(Name = "From Date")]
        public DateTime? FromDate { get; set; }

        [Display(Name = "To Date")]
        public DateTime? ToDate { get; set; }

}

following is the code in the index.cshtml

@using (Html.BeginForm("Index", "Calls", Model, FormMethod.Get))
{
<div id="filter_set_wrapper">
                    @Html.Custom().FilterLabelFor(model => model.FromDate)
                    @Html.Custom().TextBoxFor(model => model.FromDate)
                    @Ajax.TextBoxClear("FromDate", "FromDateClear(#FromDate#, ##);", "Clear From Date", "PopUpLink.PNG")
                </div>
                <div id="filter_set_wrapper">
                    @Html.Custom().FilterLabelFor(model => model.ToDate)
                    @Html.Custom().TextBoxFor(model => model.ToDate)
                    @Ajax.TextBoxClear("ToDate", "ToDateClear(#ToDate#, ##);", "Clear To Date", "PopUpLink.PNG")
                </div>

<input type="submit" class="btn" value="Filter" />
}

following is the controller method

public ActionResult Index(LogModel callsModel)开发者_如何学运维
        {
            if (callsModel.FromDate != null)
            {
                var model = callsModel;

                var d = DateTime.Parse(model.FromDate.Value.ToString("MM/dd/yyyy"));



            }
}

could anybody help me in resolving this question.The First answer is giving the correct date time.But the problem is when used with the model mentioned above could not get the correct date to the controller method.If anybody know how to fix it with above mentioned model please reply.

Thanks in advanced.


Do you use DateTime.ParseExact?

var date = DateTime.ParseExact(Request["FromDate"], "dd/MM/yy", CultureInfo.InvariantCulture);

I think should work?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号