NealR
NealR

Reputation: 10679

Add namespace to web.config in View folder

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>

Upvotes: 0

Views: 3698

Answers (1)

Jaap
Jaap

Reputation: 2332

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

Upvotes: 6

Related Questions