0

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.

Edit view

@model PatientEducationAdmin.Models.Domain.Video

@{
    ViewBag.Title = "Edit";
}
@*putting black outline on text boxes*@

@*editors location*@
<div id="div8class" style="background-color: beige; width: 936px; border-bottom-left-radius: 15px; border-bottom-right-radius: 15px">
    <h2>Edit</h2>

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)



@*edit for tags*@


        <div class="editor-field">
            <table style="text-align: center; align-content: center; float: none; margin: 10px auto; width: 500px;">
                <tr>

                    <th>@Html.DisplayNameFor(model => model.Tags[0].TagID)</th>
                    <th>@Html.DisplayNameFor(model => model.Tags)</th>

                </tr>



                @{int i = 0;}
                @using PatientEducationAdmin.Models.DAL.Repositories
                @{ TagRepository tagDB = new TagRepository();}
                @foreach (var item in Model.Tags)
                {
                    <tr>

                        <td>
                            @Html.TextBoxFor(modelitem => item.TagID, new { @readonly = "readonly" })


                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => item.TagDescription, new { @readonly = "readonly" })


                        </td>
                        <td>
                            <button type="button" onclick="delTag()">Delete</button>
                            @*want a button to delete a specific tag or disassociate it from video...*@

                        </td>

                    </tr>
                    <tr>

                        @{i++;}
                    </tr>
                }

            </table>
            <div class="editor-field">

                @Html.EditorFor(model => model.Tags[i], new { id = "tagadd" })
                <button>Add Tag</button>
            </div>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>

    }

    <div>
        @Html.ActionLink("Back to List", "SearchVideos")
    </div>

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
</div>

VideoController

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);
        }

Call Stack

mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x49 bytes 

mscorlib.dll!System.Collections.Generic.List.this[int].get(int index = 1) + 0x23 bytes

NHibernate.dll!NHibernate.Collection.Generic.PersistentGenericBag<PatientEducationAdmin.Models.Domain.Tag>.System.Collections.Generic.IList<T>.get_Item(int index = 1) + 0x87 bytes 
[Lightweight Function]  


System.Web.Mvc.dll!System.Web.Mvc.ExpressionUtil.CachedExpressionCompiler.Compiler<PatientEducationAdmin.Models.Domain.Video,PatientEducationAdmin.Models.Domain.Tag>.CompileFromFingerprint.AnonymousMethod__4(PatientEducationAdmin.Models.Domain.Video model = {PatientEducationAdmin.Models.Domain.Video}) + 0x42 bytes 


System.Web.Mvc.dll!System.Web.Mvc.ModelMetadata.FromLambdaExpression<PatientEducationAdmin.Models.Domain.Video,PatientEducationAdmin.Models.Domain.Tag>.AnonymousMethod__2() + 0x88 bytes   


System.Web.Mvc.dll!System.Web.Mvc.ModelMetadata.Model.get() + 0x3c bytes    

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" },

System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplateDelegate executeTemplate = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0x94 bytes
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
System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper},

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" },

System.Web.Mvc.Html.TemplateHelpers.TemplateHelperDelegate templateHelper = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0xe4 bytes
System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper},

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
System.Web.Mvc.dll!System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper},

System.Linq.Expressions.Expression> expression = {System.Linq.Expressions.Expression>}, object additionalViewData = { id = "tagadd" }) + 0x5e bytes

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
System.Web.WebPages.dll!System.Web.WebPages.StartPage.RunPage() + 0x12 bytes
System.Web.WebPages.dll!System.Web.WebPages.StartPage.ExecutePageHierarchy() + 0x3f bytes
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
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
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
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
System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters.AnonymousMethod_17() + 0x4b bytes
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
System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters.AnonymousMethod
_19() + 0x49 bytes
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
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction.AnonymousMethod_20() + 0x9c bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction.AnonymousMethod
_22(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x44 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x89 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x39 bytes
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_18(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x29 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod
_3(System.IAsyncResult ar = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x30 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x98 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x3c bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x29 bytes
System.Web.Mvc.dll!System.Web.Mvc.Controller.EndExecuteCore(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x2d bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod_3(System.IAsyncResult ar = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x30 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x98 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x3c bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x29 bytes
System.Web.Mvc.dll!System.Web.Mvc.Controller.EndExecute(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x28 bytes
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
System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.BeginProcessRequest.AnonymousMethod
_3(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x2e bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod__3(System.IAsyncResult ar = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}) + 0x30 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult.End() + 0x98 bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x3c bytes
System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult}, object tag = {object}) + 0x29 bytes
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
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
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext = 494414820, System.IntPtr moduleData, int flags) + 0x245 bytes
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x372 bytes
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f bytes
[Appdomain Transition]

4

1 回答 1

0

我想你的问题在这里:

@Html.EditorFor(model => model.Tags[i], new { id = "tagadd" })

如果标签列表有 1 个元素,那么i1在这一点上,这超出了model.Tags数组的范围。

您可以通过先添加一个虚拟元素来解决此问题:

@Model.Tags.Add(new Tag());
@Html.EditorFor(model => model.Tags[i], new { id = "tagadd" })
于 2013-07-10T19:59:20.790 回答