Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Mvc.HtmlHelper<ModelName> does not contain a definition for

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());
}
like image 278
NealR Avatar asked May 02 '14 20:05

NealR


3 Answers

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

like image 98
virtualuser Avatar answered Nov 15 '22 09:11

virtualuser


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
like image 23
damaben Avatar answered Nov 15 '22 07:11

damaben


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.

like image 22
iii Avatar answered Nov 15 '22 08:11

iii