6

I am trying to use Steve Sanderson's blog post about editing a variable length list. I've installed the dll via the NuGet package manager and made sure that the namespace is in the Views/web.config file. However, I the following error when I attempt to write the using statment.

System.Web.Mvc.HtmlHelper<Monet.Models.AgentTransmission> does not contain a definition 
for 'BeginCollectionItem' and no extension method 'BeginCollectionItem' accepting a first 
argument of type 'System.Web.Mvc.HtmlHelper<Monet.Models.AgentTransmission>' could be 
found (are you missing a using directive or an assmebly reference

Views/Web.config

    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Routing" />
      <add namespace="HtmlHelpers.BeginCollectionItem" />
    </namespaces>

Partial View (updated)

@model Monet.Models.AgentRelationshipCodes

@using (Html.BeginCollectionItem("AgentRelationshipCodes"))
{
    <tr>
        <td>@Html.EditorFor(model => model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
        <td>@Html.EditorFor(model => model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
        @Html.HiddenFor(model => model.ID)
        @Html.HiddenFor(model => model.RelCodeOrdinal)
    </tr>
}

Controller (just in case)

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml;
using Monet.MonetToDss;
using Monet.Common;
using Monet.Models;
using Monet.ViewModel;
using HtmlHelpers.BeginCollectionItem;

public ViewResult NewRelationshipCode()
{
    return View("AddRelationshipCodePartial", new AgentRelationshipCodes());
}
NealR
  • 10,189
  • 61
  • 159
  • 299

5 Answers5

15

Please try to close and re-open the solution for the changes to be picked up by editor. After doing that I don't get the error

System.Web.Mvc.HtmlHelper does not contain a definition for 'BeginCollectionItem' and no extension method 'BeginCollectionItem' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assmebly reference

nKn
  • 13,691
  • 9
  • 45
  • 62
virtualuser
  • 166
  • 1
  • 3
  • This is a good point, particularly if after the change, the exception still references AgentTransmission instead of AgentRelationshipCodes. – EF0 May 02 '14 at 21:36
  • This, combined with the answer above, seemed to do the trick. – NealR May 02 '14 at 21:53
4

It is a third party library by Steve Sanderson, which you have to install first from https://www.nuget.org/packages/BeginCollectionItem/:

Install-Package BeginCollectionItem
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
damaben
  • 41
  • 1
3

I needed to add

<add namespace="HtmlHelpers.BeginCollectionItem" />

to the namespaces in the web.config of the Views folder. Mine was in an "Areas" folder so I needed to add it in the Views folder there.

You can also add a using statement right on the view instead, but then you have to remember to add it to each view.

iii
  • 383
  • 1
  • 11
2

For .Net Core

Install nuget package:- https://www.nuget.org/packages/BeginCollectionItemCore

and then add this on _ViewImports.cshtml: @using HtmlHelpers.BeginCollectionItemCore;

Saurin Vala
  • 1,898
  • 1
  • 17
  • 23
1

This is a stab in the dark, but have you tried removing the index specifier [i]? You shouldn't need one when using the BeginCollectionItem helper, as far as I recall. It generates the unique index itself.


Here are a couple more resources on the helper that I found useful:

http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/ http://justmycode.blogspot.com/2012/07/learning-mvc-editing-variable-length.html



Update: Example in reference to asker's comment

    @model Monet.Models.AgentRelationshipCodes

    @using (Html.BeginCollectionItem("AgentRelationshipCodes")) @*error displays here*@
    {
        <tr>
            <td>@Html.EditorFor(m => Model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
            <td>@Html.EditorFor(m => Model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
            @Html.HiddenFor(m => Model.ID)
            @Html.HiddenFor(m => Model.RelCodeOrdinal)
        </tr>
    }    
EF0
  • 2,940
  • 4
  • 17
  • 23
  • How would I reference a property of the collection item without specifying the index? I tried `Model.AgentRelationshipCodes.RelationshipId` but that is highlighted as an error. – NealR May 02 '14 at 20:47
  • 1
    Looking at your code further, you actually want to reference the AgentRelationshipCodes model, not the AgentTransmission model. Then you would use Model.RelationshipId directly. – EF0 May 02 '14 at 20:52
  • Yeah, just saw that :) I am still getting an error on `Html.BeginCollectionItem("AgentRelationshipCodes")` however. – NealR May 02 '14 at 20:52
  • In fact if I step through the part of the code where the partial is rendered it crashed on the `@using(Html.BeginCollectionItem()` line – NealR May 02 '14 at 20:59
  • Yes, I hear you, but any issues with the way it's configured will likely be throwing an exception on that line. – EF0 May 02 '14 at 21:08
  • Unfortunately still getting the same error. Does it matter which controller the `PartialViewResult`/`ViewResult` method is coming from? – NealR May 02 '14 at 21:40
  • No it shouldn't. I also noticed that most of the examples did not explicitly return a PartialViewResult either - so I removed that comment. Can you provide an update of the current exception message? Is it referencing the AgentRelationshipCodes model now? – EF0 May 02 '14 at 21:44
  • Man... so I changed the model to `AgentRelationshipCodes` and the partial view is no longer having a problem with `Html.BeginCollectionItem`. However... it is still throwing an error on this `using` statement. Now when the program gets to this line the debugger simply blows up and I get an `AccessViolationException was unhandled` message saying `Attempted to read or write protected memory. This is often an indication that other memory is corrupt.` – NealR May 02 '14 at 21:53