"System.Web.Mvc.HtmlHelper<ModelName>"没有包含定义

6
我正在尝试使用Steve Sanderson的博客文章关于编辑可变长度列表的技术,我已经通过NuGet包管理器安装了dll,并确保该命名空间在Views/web.config文件中。然而,当我尝试编写using语句时出现以下错误。请参考:http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
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>

局部视图(更新)

@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>
}

控制器(以防万一)

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());
}
5个回答

15

请尝试关闭并重新打开解决方案,以便编辑器捕获到更改。这样做后,我就不会收到错误信息了。

System.Web.Mvc.HtmlHelper中未包含“BeginCollectionItem”的定义,也没有接受类型为“System.Web.Mvc.HtmlHelper”的第一个参数的扩展方法'BeginCollectionItem'被找到(是否缺少using指令或程序集引用?)


这是一个很好的观点,特别是如果在更改后,异常仍然引用AgentTransmission而不是AgentRelationshipCodes。 - EF0
这个与上面的答案结合起来,似乎就解决了问题。 - NealR

4

3

I needed to add

<add namespace="HtmlHelpers.BeginCollectionItem" />

将命名空间添加到 Views 文件夹下的 web.config 中。我的文件夹在 "Areas" 下,因此我需要在那里的 Views 文件夹中添加它。

你也可以直接在视图上添加 "using" 语句,但是这样需要记住在每个视图上都添加。


2

1
这只是一次尝试,但你是否尝试过删除索引说明符[i]?只要使用BeginCollectionItem助手,我记得你不需要它。它会自动生成唯一的索引。


以下是我找到的有关该助手的更多资源:

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

更新:参考提问者的评论示例。
    @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>
    }    

1
进一步查看您的代码,实际上您想引用AgentRelationshipCodes模型,而不是AgentTransmission模型。然后您可以直接使用Model.RelationshipId。 - EF0
事实上,如果我逐步执行呈现部分的代码,它会在@using(Html.BeginCollectionItem()这一行崩溃。 - NealR
是的,我听到你了,但是如果配置有任何问题,很可能会在那一行抛出异常。 - EF0
很遗憾,仍然得到相同的错误。PartialViewResult/ViewResult方法来自哪个控制器是否有影响? - NealR
哎呀...所以我把模型改成了AgentRelationshipCodes,部分视图不再出现与Html.BeginCollectionItem有关的问题。然而...它仍然在这个using语句上抛出错误。现在当程序到达这一行时,调试器就会崩溃,并且我会收到一个AccessViolationException was unhandled消息,说Attempted to read or write protected memory. This is often an indication that other memory is corrupt. - NealR
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接