title: Index out of bounds exception with a list of tags from database
body: <p>Can't seem to figure out why the List of Tags has an out of bounds exception when trying to add a tag to the video in Edit. I thought ilist automatically created a new position for an item when needed... The Tags are supposed to be associated with a given video; so, it should generate a new tag and be associated with the video. however, I have messed with it a bit such as using Session.merge instead of Session.save and that generates a new video not a new tag... but leaving the tags[i].tagdescription as is generates the outofbounds exception. I'm sure the answer is obvious but it eludes me.</p>
  
  <p>Edit view      </p>
  
  <pre><code>@model PatientEducationAdmin.Models.Domain.Video
  
  @{
      ViewBag.Title = "Edit";
  }
  @*putting black outline on text boxes*@
  
  @*editors location*@
  &lt;div id="div8class" style="background-color: beige; width: 936px; border-bottom-left-radius: 15px; border-bottom-right-radius: 15px"&gt;
      &lt;h2&gt;Edit&lt;/h2&gt;
  
      @using (Html.BeginForm())
      {
          @Html.ValidationSummary(true)
  
  
  
  @*edit for tags*@
  
  
          &lt;div class="editor-field"&gt;
              &lt;table style="text-align: center; align-content: center; float: none; margin: 10px auto; width: 500px;"&gt;
                  &lt;tr&gt;
  
                      &lt;th&gt;@Html.DisplayNameFor(model =&gt; model.Tags[0].TagID)&lt;/th&gt;
                      &lt;th&gt;@Html.DisplayNameFor(model =&gt; model.Tags)&lt;/th&gt;
  
                  &lt;/tr&gt;
  
  
  
                  @{int i = 0;}
                  @using PatientEducationAdmin.Models.DAL.Repositories
                  @{ TagRepository tagDB = new TagRepository();}
                  @foreach (var item in Model.Tags)
                  {
                      &lt;tr&gt;
  
                          &lt;td&gt;
                              @Html.TextBoxFor(modelitem =&gt; item.TagID, new { @readonly = "readonly" })
  
  
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.TextBoxFor(modelItem =&gt; item.TagDescription, new { @readonly = "readonly" })
  
  
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;button type="button" onclick="delTag()"&gt;Delete&lt;/button&gt;
                              @*want a button to delete a specific tag or disassociate it from video...*@
  
                          &lt;/td&gt;
  
                      &lt;/tr&gt;
                      &lt;tr&gt;
  
                          @{i++;}
                      &lt;/tr&gt;
                  }
  
              &lt;/table&gt;
              &lt;div class="editor-field"&gt;
  
                  @Html.EditorFor(model =&gt; model.Tags[i], new { id = "tagadd" })
                  &lt;button&gt;Add Tag&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
  
      }
  
      &lt;div&gt;
          @Html.ActionLink("Back to List", "SearchVideos")
      &lt;/div&gt;
  
      @section Scripts {
          @Scripts.Render("~/bundles/jqueryval")
      }
  &lt;/div&gt;
  </code></pre>
  
  <p>VideoController</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using PatientEducationAdmin.Models.Domain;
  using PatientEducationAdmin.Models;
  using PatientEducationAdmin.Models.DAL.Repositories;
  
  
  namespace PatientEducationAdmin.Controllers
  {
      public class VideoController : Controller
      {
  
          private VideoRepository dbVideo = new VideoRepository();
          private TagRepository dbTag = new TagRepository();
  
  
          //
          // GET: /CRUD/Edit/5
  
          public ActionResult Edit(int id = 0)
          {
              Video video = dbVideo.GetVideo(id);
  
              if (video == null)
              {
                  return HttpNotFound();
              }
              return View(video);
          }
  
          //
          // POST: /CRUD/Edit/5
  
          [HttpPost]
          public ActionResult Edit(Video video, Tag[] tags)
          {
  
              if (ModelState.IsValid)
              {
  
                  foreach (var tag in video.Tags)
                  {
                      dbTag.Save(tag);
                      tag.Videos.Add(video);
                  }
  
                  dbVideo.Save(video);
  
                  return RedirectToAction("SearchVideos");
              }
              return View(video);
          }
  </code></pre>
  
  <p>Call Stack</p>
  
  <pre><code>mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x49 bytes 
  </code></pre>
  
  <p>mscorlib.dll!System.Collections.Generic.List.this[int].get(int index = 1) + 0x23 bytes </p>
  
  <pre><code>NHibernate.dll!NHibernate.Collection.Generic.PersistentGenericBag&lt;PatientEducationAdmin.Models.Domain.Tag&gt;.System.Collections.Generic.IList&lt;T&gt;.get_Item(int index = 1) + 0x87 bytes 
  [Lightweight Function]  
  
  
  System.Web.Mvc.dll!System.Web.Mvc.ExpressionUtil.CachedExpressionCompiler.Compiler&lt;PatientEducationAdmin.Models.Domain.Video,PatientEducationAdmin.Models.Domain.Tag&gt;.CompileFromFingerprint.AnonymousMethod__4(PatientEducationAdmin.Models.Domain.Video model = {PatientEducationAdmin.Models.Domain.Video}) + 0x42 bytes 
  
  
  System.Web.Mvc.dll!System.Web.Mvc.ModelMetadata.FromLambdaExpression&lt;PatientEducationAdmin.Models.Domain.Video,PatientEducationAdmin.Models.Domain.Tag&gt;.AnonymousMethod__2() + 0x88 bytes   
  
  
  System.Web.Mvc.dll!System.Web.Mvc.ModelMetadata.Model.get() + 0x3c bytes    
  </code></pre>
  
  <p>System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper}, </p>
  
  <p>System.Web.Mvc.ModelMetadata metadata = {System.Web.Mvc.CachedDataAnnotationsModelMetadata}, string htmlFieldName = "Tags[1]", string templateName = null, System.Web.UI.WebControls.DataBoundControlMode mode = Edit, object additionalViewData = { id = "tagadd" }, </p>
  
  <p>System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplateDelegate executeTemplate = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0x94 bytes<br>
      System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper}, System.Web.Mvc.ModelMetadata metadata = {System.Web.Mvc.CachedDataAnnotationsModelMetadata}, string htmlFieldName = "Tags[1]", string templateName = null, System.Web.UI.WebControls.DataBoundControlMode mode = Edit, object additionalViewData = { id = "tagadd" }) + 0x5f bytes<br>
      System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper}, </p>
  
  <p>System.Linq.Expressions.Expression> expression = {System.Linq.Expressions.Expression>}, string templateName = null, string htmlFieldName = null, System.Web.UI.WebControls.DataBoundControlMode mode = Edit, object additionalViewData = { id = "tagadd" }, </p>
  
  <p>System.Web.Mvc.Html.TemplateHelpers.TemplateHelperDelegate templateHelper = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0xe4 bytes<br>
      System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper}, </p>
  
  <p>System.Linq.Expressions.Expression> expression = {System.Linq.Expressions.Expression>}, string templateName = null, string htmlFieldName = null, System.Web.UI.WebControls.DataBoundControlMode mode = Edit, object additionalViewData = { id = "tagadd" }) + 0x8d bytes<br>
      System.Web.Mvc.dll!System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper}, </p>
  
  <p>System.Linq.Expressions.Expression> expression = {System.Linq.Expressions.Expression>}, object additionalViewData = { id = "tagadd" }) + 0x5e bytes   </p>
  
  <blockquote>
    <p>App_Web_gfall1nh.dll!ASP._Page_Views_Video_Edit_cshtml.Execute() Line 138 + 0x27b bytes C#
        System.Web.WebPages.dll!System.Web.WebPages.WebPageBase.ExecutePageHierarchy() + 0xc6 bytes 
        System.Web.Mvc.dll!System.Web.Mvc.WebViewPage.ExecutePageHierarchy() + 0x78 bytes<br>
        System.Web.WebPages.dll!System.Web.WebPages.StartPage.RunPage() + 0x12 bytes<br>
        System.Web.WebPages.dll!System.Web.WebPages.StartPage.ExecutePageHierarchy() + 0x3f bytes<br>
        System.Web.WebPages.dll!System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext pageContext, System.IO.TextWriter writer, System.Web.WebPages.WebPageRenderingBase startPage) + 0x4d bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.RazorView.RenderView(System.Web.Mvc.ViewContext viewContext = {System.Web.Mvc.ViewContext}, System.IO.TextWriter writer = {System.Web.HttpWriter}, object instance = {ASP._Page_Views_Video_Edit_cshtml}) + 0x2e8 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.BuildManagerCompiledView.Render(System.Web.Mvc.ViewContext viewContext = {System.Web.Mvc.ViewContext}, System.IO.TextWriter writer = {System.Web.HttpWriter}) + 0x17f bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.ViewResultBase.ExecuteResult(System.Web.Mvc.ControllerContext context = {System.Web.Mvc.ControllerContext}) + 0x1b0 bytes 
        System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(System.Web.Mvc.ControllerContext controllerContext = {System.Web.Mvc.ControllerContext}, System.Web.Mvc.ActionResult actionResult = {System.Web.Mvc.ViewResult}) + 0x28 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters.AnonymousMethod_<em>17() + 0x4b bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter filter = {PatientEducationAdmin.Controllers.VideoController}, System.Web.Mvc.ResultExecutingContext preContext = {System.Web.Mvc.ResultExecutingContext}, System.Func continuation = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0xb2 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters.AnonymousMethod</em>_19() + 0x49 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(System.Web.Mvc.ControllerContext controllerContext = {System.Web.Mvc.ControllerContext}, System.Collections.Generic.IList filters = Count = 1, System.Web.Mvc.ActionResult actionResult = {System.Web.Mvc.ViewResult}) + 0x130 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction.AnonymousMethod_<em>20() + 0x9c bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction.AnonymousMethod</em>_22(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x44 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x89 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x39 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x29 bytes 
        System.Web.Mvc.dll!System.Web.Mvc.Controller.BeginExecuteCore.AnonymousMethod_<em>18(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x29 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod</em>_3(System.IAsyncResult ar = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x30 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x98 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x3c bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x29 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Controller.EndExecuteCore(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x2d bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod_<em>3(System.IAsyncResult ar = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x30 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x98 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x3c bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x29 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Controller.EndExecute(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x28 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x28 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.BeginProcessRequest.AnonymousMethod</em>_3(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x2e bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod__3(System.IAsyncResult ar = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x30 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x98 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x3c bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x29 bytes<br>
        System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x29 bytes 
        System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult result = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x27 bytes 
        System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x242 bytes<br>
        System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.CallHandlerExecutionStep}, ref bool completedSynchronously = false) + 0x9c bytes 
        System.Web.dll!System.Web.HttpApplication.PipelineStepManager.ResumeSteps(System.Exception error) + 0x474 bytes 
        System.Web.dll!System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext context, System.AsyncCallback cb) + 0x60 bytes 
        System.Web.dll!System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest wr = {System.Web.Hosting.IIS7WorkerRequest}, System.Web.HttpContext context = {System.Web.HttpContext}) + 0xbb bytes<br>
        System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext = 494414820, System.IntPtr moduleData, int flags) + 0x245 bytes<br>
        System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f bytes<br>
        [Native to Managed Transition]<br>
        [Managed to Native Transition]<br>
        System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x372 bytes<br>
        System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f bytes<br>
        [Appdomain Transition]  </p>
  </blockquote>