title: Maintaining state of listbox in MVC
body: <p>I have dual list box in <strong>MVC</strong>. Id's of the all items in selected listbox needs to be posted the server. Model has three properties defined</p>
  
  <pre><code>IEnumerable&lt;SelectListItem&gt; AvailItems {get;set;}
  IEnumerable&lt;SelectListItem&gt; SelectedItems {get;set;}
  string SelectedItemIds {get;set;} 
  </code></pre>
  
  <p>I have populated <code>availItems</code> and <code>selectedItems</code> listboxes with different names and <code>SelectedItemIds</code> property as hiddenfield. Javascript updates the hiddenfield if the user add/removes items from the listbox. In httpost, I receive only <code>selectedItemIds</code> since name of listboxes are changed, which is expected behavior. </p>
  
  <p>Problem is when the model is invalid or any exception occurs, return <code>View(model)</code> gets executed, the listbox populates empty values but <code>hiddenfield</code> contains values of modified items. And when again user submits the page, the values from <code>hiddenfield</code> get posted but displayed items will be from db. I followed this approach of using <code>hiddenfield</code> instead of selecting all items before submit (using javascript) because it doesn't harm if non-js user submits the page. The values from <code>hiddenfield</code> gets pickedup.</p>
  
  <p>Any ideas of how to deal with above situation?</p>