title: Html Helper to get values from Model State
body: <p>Asp.Net MVC 3</p>
  
  <p>I seem to have a similar problem as this post answered by Darin Dimitrov. So, Darin if you are reading this, please help :)</p>
  
  <p><a href="https://stackoverflow.com/questions/7164235/asp-net-mvc2-strongly-typed-helpers-not-using-model">asp.net-mvc2 - Strongly typed helpers not using Model?</a></p>
  
  <p>The problem I have is I am looking for an html helper that will contain the posted value in the modelstate. </p>
  
  <p>For example, if I use an editor for like this:</p>
  
  <pre><code>@Html.EditorFor(model =&gt; model.SelectedTags)
  </code></pre>
  
  <p>I can see the value that was posted. The problem is I need a way to get this value without creating a textbox, I just want the string because I need it in some javascript.</p>
  
  <p>I've tried DisplayFor, but that doesn't contain the posted value:</p>
  
  <pre><code>@Html.DisplayFor(model =&gt; model.SelectedTags)
  </code></pre>
  
  <p>By the way, I don't find this behavior intuitive AT ALL. I spent a few hours debugging ModelStateToTempDataAttribute from MVCContrib thinking it was a bug in their code to Import/Export Model State.</p>
  
  <p>Thanks for any help!</p>
  
  <h2>Edit - Added Repro Code</h2>
  
  <p>Take these steps to reproduce:</p>
  
  <ol>
  <li>Start project. Property1 should be blank (required), Property2 should have "abc"</li>
  <li>Change Property2 to "xxx"</li>
  <li>Submit Form (notice ClientValidationEnabled is False)</li>
  <li>Form is posted, redirect, load (PRG). Property2 textbox has"xxx" and right below you will see "abc" from DisplayFor.</li>
  </ol>
  
  <p>Controller</p>
  
  <pre><code>[ModelStateToTempData] //From MVCContrib
  public class HomeController : Controller
  {
  
      [HttpGet]
      public ActionResult Index()
      {
          //simulate load from db
          var model = new FormModel() { MyProperty2 = "abc" }; 
  
          return View(model);
      }
  
      [HttpGet]
      public ActionResult Success()
      {
          return View();
      }
  
      [HttpPost]
      public ActionResult Index(FormModel model)
      {
          if (ModelState.IsValid)
          {
              return RedirectToAction("Success");
          }
          else
          {
              return RedirectToAction("Index");
          }
      }
  }
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>public class FormModel
  {
    [Required]
    public string MyProperty1 { get; set; }
  
    public string MyProperty2 { get; set; }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@model MvcApplication4.Models.FormModel
  @using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;FormModel&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.MyProperty1)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.MyProperty1)
          @Html.ValidationMessageFor(model =&gt; model.MyProperty1)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.MyProperty2)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.MyProperty2)
          @Html.ValidationMessageFor(model =&gt; model.MyProperty2)
      &lt;/div&gt;
  
      @Html.DisplayFor(model =&gt; model.MyProperty2)
  
      &lt;p&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>Config:</p>
  
  <pre><code>&lt;add key="ClientValidationEnabled" value="false" /&gt;
  </code></pre>
  
  <p>ModelStateToTempData (MVCContrib):</p>
  
  <pre><code>public class ModelStateToTempDataAttribute : ActionFilterAttribute
      {
          public const string TempDataKey = "__MvcContrib_ValidationFailures__";
  
          /// &lt;summary&gt;
          /// When a RedirectToRouteResult is returned from an action, anything in the ViewData.ModelState dictionary will be copied into TempData.
          /// When a ViewResultBase is returned from an action, any ModelState entries that were previously copied to TempData will be copied back to the ModelState dictionary.
          /// &lt;/summary&gt;
          /// &lt;param name="filterContext"&gt;&lt;/param&gt;
          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
              var modelState = filterContext.Controller.ViewData.ModelState;
  
              var controller = filterContext.Controller;
  
              if(filterContext.Result is ViewResultBase)
              {
                  //If there are failures in tempdata, copy them to the modelstate
                  CopyTempDataToModelState(controller.ViewData.ModelState, controller.TempData);
                  return;
              }
  
              //If we're redirecting and there are errors, put them in tempdata instead (so they can later be copied back to modelstate)
              if((filterContext.Result is RedirectToRouteResult || filterContext.Result is RedirectResult) &amp;&amp; !modelState.IsValid)
              {
                  CopyModelStateToTempData(controller.ViewData.ModelState, controller.TempData);
              }
          }
  
          private void CopyTempDataToModelState(ModelStateDictionary modelState, TempDataDictionary tempData)
          {
              if(!tempData.ContainsKey(TempDataKey)) return;
  
              var fromTempData = tempData[TempDataKey] as ModelStateDictionary;
              if(fromTempData == null) return;
  
              foreach(var pair in fromTempData)
              {
                  if (modelState.ContainsKey(pair.Key))
                  {
                      modelState[pair.Key].Value = pair.Value.Value;
  
                      foreach(var error in pair.Value.Errors)
                      {
                          modelState[pair.Key].Errors.Add(error);
                      }
                  }
                  else
                  {
                      modelState.Add(pair.Key, pair.Value);
                  }
              }
          }
  
          private static void CopyModelStateToTempData(ModelStateDictionary modelState, TempDataDictionary tempData)
          {
              tempData[TempDataKey] = modelState;
          }
      }
  </code></pre>