在我的ASP MVC 3网站中,我尝试使用Steve Sanderson's blog post中的BeginCollectionItem
dll。我的部分视图设置如此
@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>
}
但即使BeginCollectionItem
在HtmlHelpers
命名空间下显示为选项,我也会收到以下错误:
'HtmlHelpers.BeginCollectionItem' is a 'namespace', which is not valid in the given context
我相信我在Views文件夹中的web.config中正确设置了命名空间,但是我对ASP MVC比较新,所以不是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>
答案 0 :(得分:6)
确保重建项目并重新启动Visual Studio,否则Web.config中的更改将不会生效! 请参阅此博客:http://blog.craigtp.co.uk/post/Intellisense-Web-Extensions-Razor-Views-in-ASPNET-MVC-3.aspx