title: Move data between dual listbox in asp.net mvc
body: <p>Hi I'm new in MVC now working with dual listboxes. I wanted to move the data from listbox1 to listbox2. For that I'm using the following code:</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;%:Html.HiddenFor(m=&gt;m.objMemberAccessRights.Id) %&gt;
              &lt;%:Html.ListBoxFor(model =&gt;model.objMemberAccessRights.AvailableSelected, newMultiSelectList(Model.objMemberAccessRights.AvailableNames, "Id", "InstituteName", Model.objMemberAccessRights.AvailableSelected), new { size="8"})%&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;br/&gt;
              &lt;%:Html.ActionLink("&gt;&gt;", "AddNames", "MemberDetails") %&gt;
              &lt;br/&gt;
              &lt;br/&gt;
              &lt;%:Html.ActionLink("&lt;&lt;", "RemoveNames", "MemberDetails") %&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;%:Html.ListBoxFor(model=&gt;model.objMemberAccessRights.RequestedSelected,newMultiSelectList(Model.objMemberAccessRights.RequestedNames,"Id","InstituteName",Model.objMemberAccessRights.RequestedSelected), new { size="8"}) %&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>//
  // GET: /MemberDetails/Create
  public ActionResult Create()
  {
      MemberDetailsWrapper MD = new MemberDetailsWrapper();
      MD.objMemberAccessRights = newMemberAccessRights{ AvailableNames = getAllInstituteNameList(), RequestedNames = new List&lt;InstituteInformation&gt;() };
      return View(MD);
  }
  
  //AddNames
  public ActionResult AddNames(MemberDetailsWrapper MD, string add)
  {
      ModelState.Clear();
      RestoreSavedState(MD);
      if (!add.Equals("&gt;&gt;"))
      {
          if (MD.objMemberAccessRights.AvailableSelected != null)
          {
              var names = getAllInstituteNameList().Where(p =&gt; MD.objMemberAccessRights.AvailableSelected.Contains(p.InstituteName));
              MD.objMemberAccessRights.RequestedNames.AddRange(names);
              MD.objMemberAccessRights.AvailableSelected = null;
          }
      }
      SaveState(MD);
      return View(MD);
  }
  
  //RemoveNames
  public ActionResult RemoveNames(MemberDetailsWrapper MD, string remove)
  {
      ModelState.Clear();
      RestoreSavedState(MD);
      if (!remove.Equals("&lt;&lt;"))
      {
          if (MD.objMemberAccessRights.RequestedSelected != null)
          {
              MD.objMemberAccessRights.RequestedNames.RemoveAll(p =&gt; MD.objMemberAccessRights.RequestedSelected.Contains(p.InstituteName));
              MD.objMemberAccessRights.RequestedSelected = null;
          }
          SaveState(MD);
          return View(MD);
      }
  
  #regionSupportFuncs
  public void SaveState(MemberDetailsWrapper MD)
  {
      MD.objMemberAccessRights.SavedRequested = string.Join(",", MD.objMemberAccessRights.RequestedNames.Select(p =&gt;p.InstituteName).ToArray());
      ////Available Names = All - Requested
      MD.objMemberAccessRights.AvailableNames = getAllInstituteNameList().Except(MD.objMemberAccessRights.RequestedNames).ToList();
  }
  
  //RestoreSavedState
  public void RestoreSavedState(MemberDetailsWrapper MD)
  {
      MD.objMemberAccessRights.RequestedNames = new List&lt;InstituteInformation&gt;();
      if (!string.IsNullOrEmpty(MD.objMemberAccessRights.SavedRequested))
      {
          string[] nameids = MD.objMemberAccessRights.SavedRequested.Split(',');
          var name = getAllInstituteNameList().Where(p =&gt;nameids.Contains(p.InstituteName));
          MD.objMemberAccessRights.RequestedNames.AddRange(name);
      }
  }
  #endregion
  </code></pre>
  
  <p><strong>In model:</strong></p>
  
  <pre><code>public class InstituteInformation
  {
      public int Id { get; set; }
      public string InstituteName { get; set; }
  }
  
  public class MemberAccessRights
  {
      public int Id { get; set; }
  
      public List&lt;InstituteInformation&gt; AvailableNames { get; set; }
      public string[] AvailableSelected { get; set; }
  
      public List&lt;InstituteInformation&gt; RequestedNames { get; set; }
      public string[] RequestedSelected { get; set; }
  
      public string SavedRequested { get; set; }
  }
  </code></pre>
  
  <p>Here When I click on >> button then this button returns the Error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object. Description: An
    unhandled exception occurred during the execution of the current web
    request. Please review the stack trace for more information about the
    error and where it originated in the code.   Exception Details:
    System.NullReferenceException: Object reference not set to an instance
    of an object.   </p>
  </blockquote>
  
  <pre><code>Source Error:   
  Line 170:   public void    RestoreSavedState(MemberDetailsWrapper MD)
  Line 171:   { 
  Line 172:       MD.objMemberAccessRights.RequestedNames = new List&lt;InstituteInformation&gt;();
  Line 173:
  Line 174:       if (!string.IsNullOrEmpty(MD.objMemberAccessRights.SavedRequested))
  </code></pre>