0

In my ASP MVC 3 site, I am trying to use the BeginCollectionItem dll from Steve Sanderson's blog post. I have my partial view set up like so

@model Monet.Models.DropDownValues

@using (HtmlHelpers.BeginCollectionItem("drop"))
{
    @Html.HiddenFor(model => model.Field)
    @Html.HiddenFor(model => model.DisplayPage)

    <div class="editor-label">
        @Html.LabelFor(model => model.AllowedValue)
    </div>
    <div class="label-field">
        @Html.EditorFor(model => model.AllowedValue)
        @Html.ValidationMessageFor(model => model.AllowedValue)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.DisplayValue)
    </div>
    <div class="label-field">
        @Html.EditorFor(model => model.DisplayValue)
        @Html.ValidationMessageFor(model => model.DisplayValue)
    </div>
}

However even though BeginCollectionItem appears as an option under the HtmlHelpers namespace, I get the following error:

'HtmlHelpers.BeginCollectionItem' is a 'namespace', which is not valid in the given context

I believe I have the namespace set correctly in the web.config located in the Views folder, however I'm relatively new to ASP MVC so not 100%

  <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  </sectionGroup>
</configSections>
<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <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>
  </pages>
</system.web.webPages.razor>

1 Answer 1

6

Be sure to rebuild your project AND restart Visual Studio else the change in the Web.config will not take into effect! See this blog: http://blog.craigtp.co.uk/post/Intellisense-Web-Extensions-Razor-Views-in-ASPNET-MVC-3.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. As a shortcut though, unloading and reloading the project also works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.