Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Bind TimeZoneInfo in MVC
    primarykey
    data
    text
    <p>I have the following class that I'm using for a model:</p> <pre><code>public class ApplicationUser { public int? UserId { get; set; } public TimeZoneInfo TimeZoneDefault { get; set; } public string Username { get; set; } [...] } </code></pre> <p>In the view I have the following code that successfully creates the drop-down list:</p> <pre><code>@model Acme.ApplicationUser @{ var timeZoneList = TimeZoneInfo .GetSystemTimeZones() .Select(t =&gt; new SelectListItem { Text = t.DisplayName, Value = t.Id, Selected = Model != null &amp;&amp; t.Id == Model.TimeZoneDefault.Id }); } </code></pre> <p>In the form call it with:</p> <pre><code>&lt;table&gt; [....] &lt;tr&gt; &lt;td&gt; @Html.LabelFor(model =&gt; model.TimeZoneDefault, "Default Time Zone:")&lt;/strong&gt; &lt;/td&gt; &lt;td&gt; @Html.DropDownListFor(model =&gt; model.TimeZoneDefault, timeZoneList) &lt;input type="submit" value="Save" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Everything displays correctly, the issue is back on the controller, I have this:</p> <pre><code>[HttpPost] public ActionResult Profile(ApplicationUser model) { if (ModelState.IsValid) { model.Save(); } return View(); } </code></pre> <p>The ModelState is invalid when posted back, the error is:</p> <blockquote> <p>System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.TimeZoneInfo' failed because no type converter can convert between these types.</p> </blockquote> <p>What do I need to do to convert the selected value back to a TimeZoneInfo?</p>
    singulars
    1. This table or related slice is empty.
    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