DateTime编辑器模板不以正确的格式呈现日期

时间:2022-12-15 15:30:44

I have a page with a datetime input on it, along with the customary JQuery datepicker, and an editor template for "Date" which formats the date in the text box and the date that is passed into the view and adds the required clases etc.

我有一个带有日期时间输入的页面,以及常见的JQuery日期选择器,以及“日期”的编辑器模板,它在文本框中设置日期格式,并将日期传递到视图中并添加所需的clases等。

When I call the page from the default action (which takes a nullable datetime for passing the date into), I get a populated date and it formats correctly (in this case dd/mm/yyyy). When I call the page with a datetime, it builds the date correctly in the action, but the page renders the date as "mm/dd/yyy hh:mm:ss", rather than the desired format. The url I'm calling the action with is <url>?reportDate=06%2F13%2F2012%2000%3A00%3A00, which is rendered from an html helper action link, passing in the model date as the parameter, and the action can translate this to the correct date. If I try to call the page with <url>?reportDate=13%2F06%2F2012 (the format I want to display the date as), the action takes it as a null parameter being passed, the page displays the date correctly, but a validation exception is thrown - The value '13/06/2012' is invalid.

当我从默认操作调用页面时(将日期传递到可以使用的日期时间为空),我得到一个填充日期并且格式正确(在这种情况下为dd / mm / yyyy)。当我使用日期时间调用页面时,它会在操作中正确构建日期,但页面将日期呈现为“mm / dd / yyy hh:mm:ss”,而不是所需的格式。我正在调用动作的网址是 ?reportDate = 06%2F13%2F2012%2000%3A00%3A00,它是从html帮助程序操作链接呈现的,将模型日期作为参数传递,以及操作可以将此转换为正确的日期。如果我尝试使用 ?reportDate = 13%2F06%2F2012(我希望将日期显示为格式)来调用页面,则操作将其作为传递的空参数,页面正确显示日期,但是抛出验证异常 - 值'13 / 06/2012'无效。

I'm in the UK, I've got my globalization in the web.config set to en-GB, but for some reason it's validating against the US version of the date by the looks of it.

我在英国,我在web.config中将我的全球化设置为en-GB,但出于某种原因,它通过它的外观验证了美国版本的日期。

Model:

public class ReportModel
{
    [Required]
    DataType(DataType.Date)]
    public DateTime ReportDate { get; set; }
    public string Message { get; set; }

    //More code here
}

Template (in Shared\EditorTemplates\Date.spark and using SparkViewEngine):

模板(在Shared \ EditorTemplates \ Date.spark中并使用SparkViewEngine):

<viewdata model="DateTime" />
${Html.TextBox("", string.Format("{0:d}", Model.ToShortDateString()), new { id = "", @class = "datepicker" })}

View:

<p>
    <label for="ProcessDate">
        Date
        <small>
            <span class="error">${Html.ValidationMessageFor(m => m.ReportDate)}</span>
        </small>
    </label>
    ${Html.EditorFor(m => m.ReportDate, "Date")}
</p>

Action:

public ActionResult StatementsReport(DateTime? reportDate)
{
    if (!reportDate.HasValue)
        reportDate = DateTime.Now;

    var report = new ReportModel { ReportDate = reportDate.Value.Date };
    return View("Statements/GenerateReport", report);
}

If I need to provide more detail, please let me know.

如果我需要提供更多细节,请告诉我。

Any help greatly appreciated.

任何帮助非常感谢。

1 个解决方案

#1


1  

The default model binder uses InvariantCulture with GET requests and the thread culture for POST requests when parsing. This basically means that if you are passing your reportDate action parameter in a GET request (as a query string parameter) you must use InvariantCulture to format it. Ideally use:

默认模型绑定器在解析时使用带有GET请求的InvariantCulture和用于POST请求的线程文化。这基本上意味着,如果要在GET请求中传递reportDate操作参数(作为查询字符串参数),则必须使用InvariantCulture对其进行格式化。理想情况下使用:

some_url?reportDate=yyyy-MM-dd

If you are sending a POST request (a.k.a submitting the form containing the input field), then the date must be formatted according to your application culture because this is what the model binder will use.

如果您要发送POST请求(a.k.a提交包含输入字段的表单),则必须根据您的应用程序文化格式化日期,因为这是模型绑定器将使用的。

Checkout the following blog post which illustrates this.

查看以下博客文章,说明这一点。

#1


1  

The default model binder uses InvariantCulture with GET requests and the thread culture for POST requests when parsing. This basically means that if you are passing your reportDate action parameter in a GET request (as a query string parameter) you must use InvariantCulture to format it. Ideally use:

默认模型绑定器在解析时使用带有GET请求的InvariantCulture和用于POST请求的线程文化。这基本上意味着,如果要在GET请求中传递reportDate操作参数(作为查询字符串参数),则必须使用InvariantCulture对其进行格式化。理想情况下使用:

some_url?reportDate=yyyy-MM-dd

If you are sending a POST request (a.k.a submitting the form containing the input field), then the date must be formatted according to your application culture because this is what the model binder will use.

如果您要发送POST请求(a.k.a提交包含输入字段的表单),则必须根据您的应用程序文化格式化日期,因为这是模型绑定器将使用的。

Checkout the following blog post which illustrates this.

查看以下博客文章,说明这一点。