Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC datetime culture issue when passing value back to controller
    primarykey
    data
    text
    <p><strong>How can i tell my controller/model what kind of culture it should expect for parsing a datetime?</strong></p> <p>I was using some of <a href="http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx" rel="noreferrer">this post</a> to implement jquery datepicker into my mvc application.</p> <p>When i submit the date it gets "lost in translation" i'm not using the US formatting for the date, so when it gets sent to my controller it simply becomes null.</p> <p>I have a form where the user chooses a date:</p> <pre><code>@using (Html.BeginForm("List", "Meter", FormMethod.Get)) { @Html.LabelFor(m =&gt; m.StartDate, "From:") &lt;div&gt;@Html.EditorFor(m =&gt; m.StartDate)&lt;/div&gt; @Html.LabelFor(m =&gt; m.EndDate, "To:") &lt;div&gt;@Html.EditorFor(m =&gt; m.EndDate)&lt;/div&gt; } </code></pre> <p>I've made an edit template for this, to implement the jquery datepicker:</p> <pre><code>@model DateTime @Html.TextBox("", Model.ToString("dd-MM-yyyy"), new { @class = "date" }) </code></pre> <p>I then create the datepicker widgets like this.</p> <pre><code>$(document).ready(function () { $('.date').datepicker({ dateFormat: "dd-mm-yy" }); }); </code></pre> <p>All this works fine.</p> <p>Here is where the problems start, this is my controller:</p> <pre><code>[HttpGet] public ActionResult List(DateTime? startDate = null, DateTime? endDate = null) { //This is where startDate and endDate becomes null if the dates dont have the expected formatting. } </code></pre> <p>This is why i would like to somehow tell my controller what culture it should expect? Is my model wrong? can i somehow tell it which culture to use, like with the data annotation attributes?</p> <pre><code>public class MeterViewModel { [Required] public DateTime StartDate { get; set; } [Required] public DateTime EndDate { get; set; } } </code></pre> <hr> <p>Edit: <a href="http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx" rel="noreferrer">this link</a> explains my issue and a very good solution to it aswell. Thanks to gdoron</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload