Кажется, не могу понять, почему в списке тегов есть исключение за пределами при попытке добавить тег к видео в Edit. Я думал, что ilist автоматически создает новую позицию для элемента, когда это необходимо... Теги должны быть связаны с данным видео; поэтому он должен сгенерировать новый тег и быть связан с видео. тем не менее, я немного испортил это, например, используя Session.merge вместо Session.save, и это создает новое видео, а не новый тег... но оставляя теги [i].tagdescription как есть, генерирует исключение outofbounds. Я уверен, что ответ очевиден, но он ускользает от меня.
Изменить вид
@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>
ВидеоКонтроллер
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);
}
Стек вызовов
mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x49 bytes
mscorlib.dll!System.Collections.Generic.List.this[int].get(int index = 1) + 0x23 байта
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 = {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 байта
System.Web.Mvc.dll!System.Web.Mvc.Html.TemplateHelpers.TemplateHelper (System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper}, метаданные System.Web.Mvc.ModelMetadata = {System.Web.Mvc.CachedDataAnnotationsModelMetadata}, строка htmlFieldName = "Tags [1]", строка templateName = null, System.Web.UI.WebControls.DataBoundControlMode, режим = Изменить, объект 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> выражение = {System.Linq.Expressions.Expression>}, строка templateName = null, строка htmlFieldName = null, режим System.Web.UI.WebControls.DataBoundControlMode = Edit, объект AdditionalViewData = { id = "тагадд" },
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> выражение = {System.Linq.Expressions.Expression>}, строка templateName = null, строка htmlFieldName = null, режим System.Web.UI.WebControls.DataBoundControlMode = Edit, объект AdditionalViewData = { id = "tagadd" }) + 0x8d байт
System.Web.Mvc.dll!System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper html = {System.Web.Mvc.HtmlHelper},
System.Linq.Expressions.Expression> выражение = {System.Linq.Expressions.Expression>}, объект AdditionalViewData = {id = "tagadd"}) + 0x5e байт
Я думаю, что ваша проблема здесь: