MVC3 的 jquery 日期时间选择器日期时间格式问题

发布于 2024-12-08 21:42:32 字数 2553 浏览 0 评论 0原文

我在 MVC3 应用程序中使用 JQuery 日期时间选择器。我使用日期时间来筛选列表结果。以下是我的搜索 URL 的样子

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

选择的日期时间格式是 2011 年 10 月 3 日。

但是当它在控制器中解析时,它被视为 2011 年 3 月 10 日。

选择的区域性是en-GB

但当它以 ajax 形式使用时,这个问题就不存在,因为 Ajax URL 会自动设置为 /Objectives?Description=&FromDate=10%2F03%2F2011

我使用以下代码行来格式化 DateTimePicker 中的日期时间。

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

    });
});

以下是日期时间编辑器的代码,

@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)

我们使用以下模型从用户获取数据。

public class LogModel : LogBaseModel
{

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

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

}

以下是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" />
}

以下是控制器方法

public ActionResult Index(LogModel callsModel)
        {
            if (callsModel.FromDate != null)
            {
                var model = callsModel;

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



            }
}

,任何人都可以帮助我解决这个问题。第一个答案是给出正确的日期时间。但问题是与上述模型一起使用时无法获得正确的日期到控制器方法。如果有人知道如何使用上述模型修复它,请回复。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-12-15 21:42:32

你使用 DateTime.ParseExact 吗?

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

我认为应该有效吗?

Do you use DateTime.ParseExact?

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

I think should work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文